11

十分に単純なはずですが、私には見えません。

右クリックしてポップアップ メニューを表示したコンポーネントを見つけることができます。

PopupMenu1.PopupComponent

しかし、そのメニューでクリックされた TMenuItem を含むポップアップ メニューを見つけるにはどうすればよいでしょうか。

問題を単純化して例を示します。

それぞれ異なるキャプションを持つ一連のラベルがあり、各ラベルの PopupMenu プロパティに割り当てられたポップアップ メニューがあります。

誰かがラベルの 1 つを右クリックしてポップアップ メニューを表示し、MenuItem1 をクリックすると、次のようにコーディングします。

procedure TForm1.MenuItem1Click(Sender: TObject);

begin
MsgBox (Format ('The label right-clicked has the caption %', [xxxx.Caption ])) ;
end ;

xxxx は何ですか?

実装された回答

両方の回答者に感謝します。私が最終的に得たのはこれでした:

procedure TForm1.MenuItem1Click(Sender: TObject);

var
    AParentMenu : TMenu ;
    AComponent  : TComponent ;
    ALabel      : TLabel ;

begin
AParentMenu := TMenuItem (Sender).GetParentMenu ;
AComponent  := TPopupMenu (AParentMenu).PopupComponent ;
ALabel      := TLabel (AComponent) ;
MsgBox (Format ('The label right-clicked has the caption %', [ALabel.Caption ])) ;
end ;

これは、どの TMenuItem が関係していたかを調べるため、コードの断片を提供し、変更をあまり加えずに他の OnClick ハンドラにドロップできます。

4

2 に答える 2

10

私はあなたの質問に少し混乱していますが、あなたが他のすべてを除外したので、私はあなたが探していると想像することしかできませんTMenuItem.GetParentMenu

于 2011-05-28T10:10:18.910 に答える
8
procedure TForm1.MenuItem1Click(Sender: TObject);
var pop:TPopupMenu;
    lbl:TLabel;
begin
  // Firstly get parent TPopupMenu (needs casting from TMenu) 
  pop:= TPopupMenu(MenuItem1.GetParentMenu()); 
  // pop.PopupComponent is the "source" control, just cast it to Tlabel
  lbl:= TLabel(pop.PopupComponent);            

  ShowMessage(Format('The label right-clicked has the caption %s',[lbl.Caption]));
end;
于 2011-05-28T10:57:20.543 に答える