ContextMenu.SourceControl 属性

定义

获取当前显示快捷菜单的控件。Gets the control that is displaying the shortcut menu.

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

属性值

一个 Control,它代表当前显示快捷菜单的控件。A Control that represents the control that is displaying the shortcut menu. 如果没有控件显示快捷菜单,则该属性返回 nullIf no control has displayed the shortcut menu, the property returns null.

属性

示例

下面的代码示例为 @no__t 的 @no__t 0 事件创建事件处理程序。The following code example creates an event handler for the Popup event of the ContextMenu. 事件处理程序中的代码确定两个控件中哪个控件的 PictureBox 名为 pictureBox1,一个名为 @no__t 的 TextBox 是显示快捷菜单的控件。The code in the event handler determines which of two controls a PictureBox named pictureBox1 and a TextBox named textBox1 is the control displaying the shortcut menu. 根据哪个控件引发 ContextMenu 以显示其快捷菜单,该控件将适当的 @no__t 1 对象添加到 @no__t。Depending on which control caused the ContextMenu to display its shortcut menu, the control adds the appropriate MenuItem objects to the ContextMenu. 此示例要求具有在窗体中定义的名为 contextMenu1 的 @no__t 类的实例。This example requires that you have an instance of the ContextMenu class, named contextMenu1, defined within the form. 此示例还要求将 @no__t 0 和 @no__t 添加到窗体,并将这些控件的 ContextMenu 属性设置为 contextMenu1This example also requires that you have a TextBox and PictureBox added to a form and that the ContextMenu property of these controls is set to 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 中定义的快捷菜单的控件。This property enables you to determine which control currently displays the shortcut menu defined in the ContextMenu. 如果当前未显示快捷菜单,则可以使用此属性来确定最后显示快捷菜单的控件。If the shortcut menu is not currently displayed, you can use this property to determine which control last displayed the shortcut menu. 可以在 Popup 事件中使用此属性,以确保控件显示正确的菜单项。You can use this property in the Popup event to ensure that the control displays the proper menu items. 你还可以使用此属性将对控件的引用传递给一个方法,该方法执行与快捷菜单中显示的菜单命令相关联的任务。You can also use this property to pass a reference to the control to a method that performs the tasks associated with a menu command displayed in the shortcut menu. 由于 @no__t 0 类从 Control 继承,因此,如果 ContextMenu 与窗体相关联,则也可以使用此属性。Since the Form class inherits from Control, you can also use this property if the ContextMenu is associated with a form.

适用于