イベントを使用しているコンポーネントの特定について
1つのFormに多くのボタンがある場合、あるイベントがどのボタンで使用されているかとすぐにわかる方法ありますか?
procedure TForm1.Button1Click(Sender: TObject);
とかであればButton1によって使用されているのはわかるのですが、
名前を変えられていると
どのコンポーネントで使用されているかわかりません。
正しい使い方ではないかもしれませんが、リファクタリングでも抽出できます。
メッソドの名前の変更で、新しい名前を適当なものにして、'参照を表示'にチェックして実行。
間違って適用するとえらいことになりますのでご注意。
当方Delphi2005で他のバージョンで動作するか不明ですが、
usesにTypInfoを追加して
procedure GetComponentEvent(const Owner: TForm; const Component: TComponent);
var
i, Count: Integer;
PropList: PPropList;
PropInfo: TPropInfo;
PropValue: TMethod;
begin
PropList := nil;
Count := GetPropList(Component, PropList);
try
//プロパティの数だけループ
for i := 0 to Count - 1 do
begin
PropInfo := PropList[i]^;
//プロパティがイベントの時
if PropInfo.PropType^.Kind = tkMethod then
begin
PropValue := GetMethodProp(Component, PropInfo.Name);
if PropValue.Code <> nil then
begin
ShowMessageFmt('%s.%s = %s', [Component.Name, PropInfo.Name,
Owner.MethodName(PropValue.Code)]);
end;
end;
end;
finally
if PropList <> nil then FreeMem(PropList);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
//フォームのイベントがあれば取得
GetComponentEvent(Self, Self);
//フォーム上のコントロールのイベントを取得
for i := 0 to ComponentCount - 1 do
begin
GetComponentEvent(Self, Components[i]);
end;
end;
ツイート | ![]() |