Menu.GetContextMenu 메서드

정의

이 메뉴가 들어 있는 ContextMenu를 가져옵니다.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

반환

이 메뉴가 들어 있는 ContextMenu입니다.The ContextMenu that contains this menu. 기본값은 null입니다.The default is null.

예제

이 예제를 사용 하 여 합니다 GetContextMenu 메서드를 포함 하는 바로 가기 메뉴에 대 한 참조를 가져올 menuItem1 또는 menuItem2, 메시지 상자에 바로 가기 메뉴 정보를 표시 합니다.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. 두 항목을 사용 하 여 프로그래밍 방식으로 바로 가기 메뉴를 만들 NewOpen입니다.You programmatically create a shortcut menu with two items, New and Open. 그런 다음 적절 한 이벤트 처리기를 만들어이 기능 이러한 항목에 추가 합니다.You then add functionality to these items by creating the appropriate event handlers. 예제를 실행 하는 경우 바로 가기 메뉴를 표시 하기 위해 폼을 마우스 오른쪽 단추로 클릭을 알려 주는 메시지 상자를 얻게 됩니다.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. 그런 다음 메뉴 항목을 클릭 하면 다른 메시지를 항목 했 고 정보를 포함 하는 바로 가기 메뉴에 표시를 가져옵니다.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. 이 예제에서는 이미 만든를 Form 라는 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


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


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

설명

이 메서드를 사용 하면에 대 한 참조를 가져올 수 있습니다는 ContextMenu 이 메뉴에 포함 됩니다.This method allows you to obtain a reference to the ContextMenu that this menu is contained in. 이 속성은 반환 null 메뉴에서 포함 되지 않은 경우는 ContextMenu합니다.This property returns null if the menu is not contained in a ContextMenu. 이 메뉴에 포함 된 경우 발생할 수 있습니다는 MenuItem 또는 MainMenu, 메뉴는 메뉴에 포함 되지 않은 경우 또는 합니다.This can occur if the menu is contained in a MenuItem or MainMenu, or if the menu is not contained in any menu. 여부를 메뉴를 현재 사용 중인지를 확인 하 고 위치를 확인 하려면이 속성을 사용할 수 있습니다.You can use this property to determine whether a menu is currently being used, and also to determine where.

적용 대상

추가 정보