Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Ruft das ContextMenu ab, das dieses Menü enthält.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)
Syntax
'Declaration
Public Function GetContextMenu As ContextMenu
'Usage
Dim instance As Menu
Dim returnValue As ContextMenu
returnValue = instance.GetContextMenu
public ContextMenu GetContextMenu ()
public:
ContextMenu^ GetContextMenu ()
public ContextMenu GetContextMenu ()
public function GetContextMenu () : ContextMenu
Rückgabewert
Das ContextMenu, das dieses Menü enthält. Der Standardwert ist NULL (Nothing in Visual Basic).
Hinweise
Mit dieser Methode können Sie einen Verweis auf das ContextMenu abrufen, in dem dieses Menü enthalten ist. Diese Eigenschaft gibt NULL (Nothing in Visual Basic) zurück, wenn das Menü nicht in einem ContextMenu enthalten ist. Dies kann der Fall sein, wenn das Menü in einem MenuItem bzw. MainMenu oder aber in keinem Menü enthalten ist. Mit dieser Eigenschaft können Sie ermitteln, ob und wo das Menü derzeit verwendet wird.
Beispiel
In diesem Beispiel wird die GetContextMenu-Methode verwendet, um einen Verweis auf das Kontextmenü abzurufen, das menuItem1 oder menuItem2 enthält, und die Informationen zum Kontextmenü in einem Meldungsfeld anzuzeigen. Programmgesteuert wird ein Kontextmenü mit zwei Elementen (New und Open) erstellt. Diese Elemente werden dann einsatzfähig gemacht, indem die entsprechenden Ereignishandler erstellt werden. Wenn Sie das Beispiel ausführen, werden Sie in einem Meldungsfeld aufgefordert, mit der rechten Maustaste auf das Formular zu klicken, um das Kontextmenü anzuzeigen. Nachdem Sie auf ein Menüelement geklickt haben, wird eine andere Meldung angezeigt, die zum einen mitteilt, auf welches Element geklickt wurde, und zum anderen die Informationen über das als Container fungierende Kontextmenü anzeigt. Für dieses Beispiel ist es erforderlich, dass bereits ein Form mit dem Namen Form1 erstellt wurde.
Public Sub AddContextmenu()
' Create a shortcut menu.
Dim m As New ContextMenu()
Me.ContextMenu = m
' Create MenuItem objects.
Dim menuItem1 As New MenuItem()
Dim menuItem2 As New MenuItem()
' Set the Text property.
menuItem1.Text = "New"
menuItem2.Text = "Open"
' Add menu items to the MenuItems collection.
m.MenuItems.Add(menuItem1)
m.MenuItems.Add(menuItem2)
' Display the starting message.
MessageBox.Show("Right-click the form to display the shortcut menu items")
' Add functionality to the menu items.
AddHandler menuItem1.Click, AddressOf Me.menuItem1_Click
AddHandler menuItem2.Click, AddressOf Me.menuItem2_Click
End Sub 'AddContextmenu
Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim textReport As String = "You clicked the New menu item. " + vbCr + "It is contained in the following shortcut menu: " + vbCr + vbCr
' Get information on the shortcut menu in which menuitem1 is contained.
textReport += ContextMenu.GetContextMenu().ToString()
' Display the shortcut menu information in a message box.
MessageBox.Show(textReport, "The ContextMenu Information")
End Sub 'menuItem1_Click
Private Sub menuItem2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim textReport As String = "You clicked the Open menu item. " + vbCr + "It is contained in the following shortcut menu: " + vbCr + vbCr
' Get information on the shortcut menu in which menuitem1 is contained.
textReport += ContextMenu.GetContextMenu().ToString()
' Display the shortcut menu information in a message box.
MessageBox.Show(textReport, "The ContextMenu Information")
End Sub 'menuItem2_Click
public void AddContextmenu()
{
// Create a shortcut menu.
ContextMenu m = new ContextMenu();
this.ContextMenu= m;
// Create MenuItem objects.
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
// Set the Text property.
menuItem1.Text = "New";
menuItem2.Text = "Open";
// Add menu items to the MenuItems collection.
m.MenuItems.Add(menuItem1);
m.MenuItems.Add(menuItem2);
// Display the starting message.
MessageBox.Show("Right-click the form to display the shortcut menu items");
// Add functionality to the menu items.
menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
string textReport = "You clicked the New menu item. \n" +
"It is contained in the following shortcut menu: \n\n";
// Get information on the shortcut menu in which menuitem1 is contained.
textReport += ContextMenu.GetContextMenu().ToString();
// Display the shortcut menu information in a message box.
MessageBox.Show(textReport,"The ContextMenu Information");
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
string textReport = "You clicked the Open menu item. \n" +
"It is contained in the following shortcut menu: \n\n";
// Get information on the shortcut menu in which menuitem1 is contained.
textReport += ContextMenu.GetContextMenu().ToString();
// Display the shortcut menu information in a message box.
MessageBox.Show(textReport,"The ContextMenu Information");
}
public:
[STAThread]
void AddContextmenu()
{
// Create a shortcut menu.
System::Windows::Forms::ContextMenu^ m = gcnew System::Windows::Forms::ContextMenu;
this->ContextMenu = m;
// Create MenuItem objects.
MenuItem^ menuItem1 = gcnew MenuItem;
MenuItem^ menuItem2 = gcnew MenuItem;
// Set the Text property.
menuItem1->Text = "New";
menuItem2->Text = "Open";
// Add menu items to the MenuItems collection.
m->MenuItems->Add( menuItem1 );
m->MenuItems->Add( menuItem2 );
// Display the starting message.
MessageBox::Show( "Right-click the form to display the shortcut menu items" );
// Add functionality to the menu items.
menuItem1->Click += gcnew System::EventHandler( this, &Form1::menuItem1_Click );
menuItem2->Click += gcnew System::EventHandler( this, &Form1::menuItem2_Click );
}
private:
void menuItem1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
String^ textReport = "You clicked the New menu item. \n"
"It is contained in the following shortcut menu: \n\n";
// Get information on the shortcut menu in which menuitem1 is contained.
textReport = String::Concat( textReport, this->ContextMenu->GetContextMenu()->ToString() );
// Display the shortcut menu information in a message box.
MessageBox::Show( textReport, "The ContextMenu Information" );
}
void menuItem2_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
String^ textReport = "You clicked the Open menu item. \n"
"It is contained in the following shortcut menu: \n\n";
// Get information on the shortcut menu in which menuitem1 is contained.
textReport = String::Concat( textReport, this->ContextMenu->GetContextMenu()->ToString() );
// Display the shortcut menu information in a message box.
MessageBox::Show( textReport, "The ContextMenu Information" );
}
// Create a shortcut menu.
ContextMenu m = new ContextMenu();
this.set_ContextMenu(m);
// Create MenuItem objects.
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
// Set the Text property.
menuItem1.set_Text("New");
menuItem2.set_Text("Open");
// Add menu items to the MenuItems collection.
m.get_MenuItems().Add(menuItem1);
m.get_MenuItems().Add(menuItem2);
// Display the starting message.
MessageBox.Show("Right-click the form to display the shortcut menu "
+ " items");
// Add functionality to the menu items.
menuItem1.add_Click(new System.EventHandler(this.menuItem1_Click));
menuItem2.add_Click(new System.EventHandler(this.menuItem2_Click));
} //AddContextmenu
private void menuItem1_Click(Object sender, System.EventArgs e)
{
String textReport = "You clicked the New menu item. \n"
+ "It is contained in the following shortcut menu: \n\n";
// Get information on the shortcut menu in which menuitem1 is contained.
textReport += get_ContextMenu().ToString();
// Display the shortcut menu information in a message box.
MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem1_Click
private void menuItem2_Click(Object sender, System.EventArgs e)
{
String textReport = "You clicked the Open menu item. \n"
+ "It is contained in the following shortcut menu: \n\n";
// Get information on the shortcut menu in which menuitem1 is contained.
textReport += get_ContextMenu().ToString();
// Display the shortcut menu information in a message box.
MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem2_Click
public void AddContextmenu()
{
// Create a shortcut menu.
ContextMenu m = new ContextMenu();
this.set_ContextMenu(m);
// Create MenuItem objects.
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
// Set the Text property.
menuItem1.set_Text("New");
menuItem2.set_Text("Open");
// Add menu items to the MenuItems collection.
m.get_MenuItems().Add(menuItem1);
m.get_MenuItems().Add(menuItem2);
// Display the starting message.
MessageBox.Show("Right-click the form to display the shortcut menu "
+ " items");
// Add functionality to the menu items.
menuItem1.add_Click(new System.EventHandler(this.menuItem1_Click));
menuItem2.add_Click(new System.EventHandler(this.menuItem2_Click));
} //AddContextmenu
private void menuItem1_Click(Object sender, System.EventArgs e)
{
String textReport = "You clicked the New menu item. \n"
+ "It is contained in the following shortcut menu: \n\n";
// Get information on the shortcut menu in which menuitem1 is contained.
textReport += get_ContextMenu().ToString();
// Display the shortcut menu information in a message box.
MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem1_Click
private void menuItem2_Click(Object sender, System.EventArgs e)
{
String textReport = "You clicked the Open menu item. \n"
+ "It is contained in the following shortcut menu: \n\n";
// Get information on the shortcut menu in which menuitem1 is contained.
textReport += get_ContextMenu().ToString();
// Display the shortcut menu information in a message box.
MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem2_Click
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
Menu-Klasse
Menu-Member
System.Windows.Forms-Namespace
GetMainMenu