Menu.GetContextMenu Menu.GetContextMenu Menu.GetContextMenu Menu.GetContextMenu Method

Definizione

Ottiene l'oggetto ContextMenu che contiene questo menu.Gets the ContextMenu that contains this menu.

public:
 System::Windows::Forms::ContextMenu ^ GetContextMenu();
public System.Windows.Forms.ContextMenu GetContextMenu ();
member this.GetContextMenu : unit -> System.Windows.Forms.ContextMenu
Public Function GetContextMenu () As ContextMenu

Restituisce

Oggetto ContextMenu che contiene questo menu.The ContextMenu that contains this menu. Il valore predefinito è null.The default is null.

Esempi

In questo esempio, si utilizza il GetContextMenu metodo per ottenere un riferimento al menu di scelta rapida contenente menuItem1 o menuItem2e visualizzare le informazioni sul menu di scelta rapida in una finestra di messaggio.In this example, you use the GetContextMenu method to obtain a reference to the shortcut menu that contains menuItem1 or menuItem2, and display the shortcut menu information in a message box. A livello di codice crea un menu di scelta rapida con gli elementi di due New e Open.You programmatically create a shortcut menu with two items, New and Open. Aggiungere quindi funzionalità per questi elementi tramite la creazione di gestori eventi appropriati.You then add functionality to these items by creating the appropriate event handlers. Quando si esegue l'esempio, si ottiene una finestra di messaggio che invita a fare clic sul form per visualizzare il menu di scelta rapida.When you run the example, you get a message box that tells you to right-click the form in order to display the shortcut menu. Quindi, quando si fa clic su una voce di menu, si ottiene un altro messaggio che indica quale elemento è stato fatto clic e visualizza le informazioni sul menu di scelta rapida che lo contiene.Then, when you click a menu item, you get another message that tells which item has been clicked and displays the information on the containing shortcut menu. Questo esempio richiede che sia già stato creato un Form denominato Form1.This example requires that you have already created a Form named Form1.

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" );
   }
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 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

Commenti

Questo metodo consente di ottenere un riferimento di ContextMenu contenute in questo menu.This method allows you to obtain a reference to the ContextMenu that this menu is contained in. Questa proprietà restituisce null se il menu di scelta non è contenuta un ContextMenu.This property returns null if the menu is not contained in a ContextMenu. Ciò può verificarsi se il menu di scelta è contenuta in un MenuItem o MainMenu, o se il menu di scelta non è contenuta in un menu di scelta.This can occur if the menu is contained in a MenuItem or MainMenu, or if the menu is not contained in any menu. È possibile utilizzare questa proprietà per determinare se un menu di scelta è attualmente in uso, nonché per determinare la posizione.You can use this property to determine whether a menu is currently being used, and also to determine where.

Si applica a

Vedi anche