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: