User Interface / Widgets / Context Popups / Context Menu
In This Topic
    Context Menu
    In This Topic

    The context menu is a drop down menu opened in context fashion. To show a context menu you should do the following things:

    1. Create an instance of the NMenu class
    2. Populate it by adding menu items to its Items collection
    3. Use the OpenInContext static method of the NPopupWindow class to open the menu in context fashion passing the following arguments to it:
      • popupWindow - the popup window to open, you can simply create a new popup window passing the menu to the constructor, for example: new NPopupWindow(menu)
      • targetNode - the node to open the context menu for.
      • screenPos - the screen position of the top left corner of the context menu.
     Code Example

    The following piece of code demonstrates how to create and show a context menu on right click:

    Context Menu Example
    Copy Code
    NLabel label = new NLabel("Right Click Me");
    label.HorizontalPlacement = ENHorizontalPlacement.Left;
    label.VerticalPlacement = ENVerticalPlacement.Top;
    label.Background = new NBackground(NColor.PapayaWhip);
    label.Border = NBorder.CreateFilledBorder(NColor.Black);
    label.BorderThickness = new NMargins(1);
    label.MouseDown += new Function<NMouseButtonEventArgs>(OnLabelMouseDown);
     
    private void OnLabelMouseDown(NMouseButtonEventArgs arg1)
    {
        // Check whether the right mose down was used
        if (arg1.Button != ENMouseButtons.Right)
            return;
     
        // Mark the event as handled
        arg1.Cancel = true;
     
        // Create the menu to show as context menu
        NImage image = new NImage(new NUri(@"C:\FileOpen.png"));
        NMenu menu = new NMenu();
        menu.Items.Add(new NMenuItem("1) Text Only Item"));
        menu.Items.Add(new NMenuItem("2) Text Only Item"));
        menu.Items.Add(new NMenuItem(image, "3) Image and Text Item"));
        menu.Items.Add(new NCheckableMenuItem(null, "4) Checkable Item", true));
     
        // Open the menu as context menu
        NPopupWindow.OpenInContext(new NPopupWindow(menu), arg1.CurrentTargetNode, arg1.ScreenPosition);
    }
    

     When the user right clicks on the label, the following context menu will open:

    See Also