ContextMenu.SourceControl プロパティ

定義

ショートカット メニューを表示しているコントロールを取得します。

public:
 property System::Windows::Forms::Control ^ SourceControl { System::Windows::Forms::Control ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.Control SourceControl { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SourceControl : System.Windows.Forms.Control
Public ReadOnly Property SourceControl As Control

プロパティ値

ショートカット メニューを表示しているコントロールを表す Control。 ショートカット メニューを表示しているコントロールがない場合、このプロパティは null を返します。

属性

次のコード例では、 の イベントのイベント ハンドラーをPopupContextMenu作成します。 イベント ハンドラーのコードは、2 つのコントロールのうち、 という名前の コントロールPictureBoxTextBox、 という名前pictureBox1textBox1の コントロールのうち、ショートカット メニューを表示するコントロールを決定します。 がショートカット メニューを表示する原因となった ContextMenu コントロールに応じて、適切な MenuItem オブジェクトが に ContextMenu追加されます。 この例では、 という名前contextMenu1の クラスのインスタンスがContextMenuフォーム内で定義されている必要があります。 この例では、 をフォーム TextBox に追加し PictureBox 、これらのコントロールの プロパティを ContextMenu に設定する contextMenu1必要もあります。

private:
   void MyPopupEventHandler( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Define the MenuItem objects to display for the TextBox.
      MenuItem^ menuItem1 = gcnew MenuItem( "&Copy" );
      MenuItem^ menuItem2 = gcnew MenuItem( "&Find and Replace" );
      // Define the MenuItem object to display for the PictureBox.
      MenuItem^ menuItem3 = gcnew MenuItem( "C&hange Picture" );
      
      // Clear all previously added MenuItems.
      contextMenu1->MenuItems->Clear();

      if ( contextMenu1->SourceControl == textBox1 )
      {
         
         // Add MenuItems to display for the TextBox.
         contextMenu1->MenuItems->Add( menuItem1 );
         contextMenu1->MenuItems->Add( menuItem2 );
      }
      else if ( contextMenu1->SourceControl == pictureBox1 )
      {
         // Add the MenuItem to display for the PictureBox.
         contextMenu1->MenuItems->Add( menuItem3 );
      }
   }
private void MyPopupEventHandler(System.Object sender, System.EventArgs e)
 {
    // Define the MenuItem objects to display for the TextBox.
    MenuItem menuItem1 = new MenuItem("&Copy");
    MenuItem menuItem2 = new MenuItem("&Find and Replace");
    // Define the MenuItem object to display for the PictureBox.
    MenuItem menuItem3 = new MenuItem("C&hange Picture");

    // Clear all previously added MenuItems.
    contextMenu1.MenuItems.Clear();
 
    if(contextMenu1.SourceControl == textBox1)
    {
       // Add MenuItems to display for the TextBox.
       contextMenu1.MenuItems.Add(menuItem1);
       contextMenu1.MenuItems.Add(menuItem2);
    }
    else if(contextMenu1.SourceControl == pictureBox1)
    {
       // Add the MenuItem to display for the PictureBox.
       contextMenu1.MenuItems.Add(menuItem3);
    }
 }
Private Sub MyPopupEventHandler(sender As System.Object, e As System.EventArgs)
    ' Define the MenuItem objects to display for the TextBox.
    Dim menuItem1 As New MenuItem("&Copy")
    Dim menuItem2 As New MenuItem("&Find and Replace")
    ' Define the MenuItem object to display for the PictureBox.
    Dim menuItem3 As New MenuItem("C&hange Picture")
    
    ' Clear all previously added MenuItems.
    contextMenu1.MenuItems.Clear()
    
    If contextMenu1.SourceControl Is textBox1 Then
        ' Add MenuItems to display for the TextBox.
        contextMenu1.MenuItems.Add(menuItem1)
        contextMenu1.MenuItems.Add(menuItem2)
    ElseIf contextMenu1.SourceControl Is pictureBox1 Then
        ' Add the MenuItem to display for the PictureBox.
        contextMenu1.MenuItems.Add(menuItem3)
    End If
End Sub

注釈

このプロパティを使用すると、 で定義されているショートカット メニューを現在表示しているコントロールを ContextMenu確認できます。 ショートカット メニューが現在表示されていない場合は、このプロパティを使用して、ショートカット メニューを最後に表示したコントロールを決定できます。 イベントでこのプロパティを Popup 使用して、コントロールに適切なメニュー項目が表示されるようにすることができます。 また、このプロパティを使用して、ショートカット メニューに表示されるメニュー コマンドに関連付けられたタスクを実行するメソッドにコントロールへの参照を渡すこともできます。 クラスは Form から Control継承されるため、 がフォームに関連付けられている場合 ContextMenu は、このプロパティを使用することもできます。

適用対象