イベントを使用しているコンポーネントの特定


たんの  2012-05-24 18:40:55  No: 42298

イベントを使用しているコンポーネントの特定について

1つのFormに多くのボタンがある場合、あるイベントがどのボタンで使用されているかとすぐにわかる方法ありますか?

procedure TForm1.Button1Click(Sender: TObject);

とかであればButton1によって使用されているのはわかるのですが、
名前を変えられていると
どのコンポーネントで使用されているかわかりません。


Nov  2012-05-24 20:30:22  No: 42299

正しい使い方ではないかもしれませんが、リファクタリングでも抽出できます。
メッソドの名前の変更で、新しい名前を適当なものにして、'参照を表示'にチェックして実行。
間違って適用するとえらいことになりますのでご注意。


takana  2012-05-25 07:55:00  No: 42300

当方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;


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加