ContextMenu.SourceControl 属性

获取当前显示快捷菜单的控件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public ReadOnly Property SourceControl As Control
用法
Dim instance As ContextMenu
Dim value As Control

value = instance.SourceControl
public Control SourceControl { get; }
public:
property Control^ SourceControl {
    Control^ get ();
}
/** @property */
public Control get_SourceControl ()
public function get SourceControl () : Control

属性值

一个 Control,它代表当前显示快捷菜单的控件。如果没有控件显示快捷菜单,则该属性返回 空引用(在 Visual Basic 中为 Nothing)。

备注

该属性使您能够确定当前显示 ContextMenu 定义的快捷菜单的控件。如果当前没有显示快捷菜单,则可使用该属性确定上次显示该快捷菜单的控件。可在 Popup 事件中使用该属性以确保控件显示正确的菜单项。还可使用该属性向方法传递对控件的引用,该方法执行与快捷菜单中显示的菜单命令关联的任务。由于 Form 类继承自 Control,如果 ContextMenu 与窗体关联,则也可以使用该属性。

示例

下面的代码示例为 ContextMenuPopup 事件创建事件处理程序。事件处理程序中的代码决定以下两个控件中哪一个是显示快捷菜单的控件:名为 pictureBox1PictureBox 和名为 textBox1TextBox。根据哪个控件导致 ContextMenu 显示其快捷菜单,控件向 ContextMenu 添加相应的 MenuItem 对象。此示例要求窗体内定义了一个名为 contextMenu1ContextMenu 类的实例。此示例还要求向窗体添加 TextBoxPictureBox,而且这些控件的 ContextMenu 属性均设置为 contextMenu1

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 'MyPopupEventHandler '
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:
   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(Object sender, 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.get_MenuItems().Clear();
    if (contextMenu1.get_SourceControl().Equals(textBox1)) {
        // Add MenuItems to display for the TextBox.
        contextMenu1.get_MenuItems().Add(menuItem1);
        contextMenu1.get_MenuItems().Add(menuItem2);
    }
    else {
        if (contextMenu1.get_SourceControl().Equals(pictureBox1)) {
            // Add the MenuItem to display for the PictureBox.
            contextMenu1.get_MenuItems().Add(menuItem3);
        }
    }
} //MyPopupEventHandler

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

ContextMenu 类
ContextMenu 成员
System.Windows.Forms 命名空间