Menu.GetContextMenu Yöntem

Tanım

Bu menüyü ContextMenu içeren öğesini alır.

C#
public System.Windows.Forms.ContextMenu GetContextMenu();

Döndürülenler

ContextMenu Bu menüyü içeren. Varsayılan değer: null.

Örnekler

Bu örnekte, veya menuItem2içeren menuItem1 kısayol menüsüne başvuru almak ve kısayol menüsü bilgilerini bir ileti kutusunda görüntülemek için yöntemini kullanırsınızGetContextMenu. program aracılığıyla ve iki öğe NewOpeniçeren bir kısayol menüsü oluşturursunuz. Ardından uygun olay işleyicilerini oluşturarak bu öğelere işlevsellik eklersiniz. Örneği çalıştırdığınızda, kısayol menüsünü görüntülemek için forma sağ tıklamanızı söyleyen bir ileti kutusu alırsınız. Ardından, bir menü öğesine tıkladığınızda, hangi öğeye tıklandığını belirten ve içeren kısayol menüsünde bilgileri görüntüleyen başka bir ileti alırsınız. Bu örnek için zaten adlı Form1bir Form oluşturmuş olmanız gerekir.

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

Açıklamalar

Bu yöntem, bu menünün ContextMenu içinde bulunduğuna bir başvuru elde etmenizi sağlar. Menü bir ContextMenuiçinde yer almadıysa, bu özellik döndürürnull. Menü veya MainMenuiçinde yer alırsa veya menü herhangi bir MenuItem menüde yer almadıysa bu durum oluşabilir. Bu özelliği, bir menünün şu anda kullanılıp kullanılmadığını belirlemek ve ayrıca nerede kullanıldığını belirlemek için kullanabilirsiniz.

Şunlara uygulanır

Ürün Sürümler
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 10

Ayrıca bkz.