I have created a Table layout in windows forms as shown in figure, i have added a right mouse button click Menu to my table,
i wnt to add submenu items to following menuitems such as
When i click add device it should show subitems such as sounder, MCP, strobe etc
When i click fault it should show subitems such as open circuit fault, sounder fault, `short circuit fault' so on
This is how i created menuitems
ContextMenu m = new ContextMenu();
MenuItem addDevice = new MenuItem("Add Device");
MenuItem deleteDevice = new MenuItem("delete Device");
MenuItem fire = new MenuItem("Fire");
MenuItem fault = new MenuItem("Fault");
MenuItem suppress = new MenuItem("Suppress");
m.MenuItems.AddRange(new MenuItem[] { addDevice, deleteDevice, fire, fault,suppress});
tableLayout.ContextMenu = m;
m.Show((Control)(sender), e.Location);
The below is my form and table layout
You can add MenuItems to existing MenuItem like:
MenuItem addDevice = new MenuItem("Add Device");
addDevice.MenuItems.Add( new MenuItem("Add More .."));
It would be visible like:
MenuItem newMenuItem1 = new MenuItem("Employee Master");
MenuItem mnuSubMenuItem = new MenuItem();
newMenuItem1.ChildItems.Add(mnuSubMenuItem);
mnuSubMenuItem.Text = "Add User...";
mnuSubMenuItem.NavigateUrl = "ADDURL.aspx";
MenuItem mnuSubMenuItem1 = new MenuItem();
newMenuItem1.ChildItems.Add(mnuSubMenuItem1);
mnuSubMenuItem1.Text = "Edit User...";
mnuSubMenuItem1.NavigateUrl = "EDITURL.aspx";
this.MenuMaster.Items.Add(newMenuItem1);
You can add This
var someMenu= new MenuItem("Foo");
someMenu.MenuItems.Add( new MenuItem("foo "));
That format doesn't work for me. I'm forced to use:
// People often exclude the applicable "using" statements--so samples don't work!
using System.Web.UI.WebControls;
// Create the MAIN menu item
MenuItem mnuMenuItem = new MenuItem();
// Create the SUB menu item
MenuItem mnuSubMenuItem = new MenuItem();
// Create the SUB menu item, "under" the MAIN menu item!
mnuMenuItem.ChildItems.Add(mnuSubMenuItem);
Related
I have this menu like this:
// open context menu
var contextMenu = new ContextMenu();
var versionsMenu = new ToolStripDropDownMenu() {Text = "Version"};
StaticHelpers.GetPackageVersions(textBlockSelected.Text).ForEach(f=> versionsMenu.Items.Add(f));
var scheduleMenu = new ToolStripMenuItem {Text = "Schedule"};
var argumentsMenu = new ToolStripMenuItem() {Text = "Arguments"};
var removeMenu = new ToolStripMenuItem {Text = "Remove"};
//show context menu
contextMenu.Items.Add(versionsMenu);
contextMenu.Items.Add(scheduleMenu);
contextMenu.Items.Add(argumentsMenu);
contextMenu.Items.Add(removeMenu);
//add handlers
// executeMenu.Click += (o, args) => { ExecutePackage(sender); };
//open context menu
contextMenu.IsOpen = true;
I have tried MenuItems instead of ToolStripMenuItem or ToolStripDropDownMenu but could not find any documentation or examples anywhere about how you can make these things nested, for example, when the user right clicks on one of my controls I want to show this menu:
Version
Schedule
Arguments
Remove
If the user hovers over version, I want another contextMenu to extend and show the following:
V1.0
V1.1
V1.2
How can I achieve this functionality?
To add a sub-menu, you take an existing item and do the same to it:
var versionsMenu = new ToolStripMenuItem();
versionsMenu.DropDownItems.Add(nestedItem);
I want to open a WPF ContextMenu when the user clicks the system tray icon. With Windows Forms this is straight-forward, just call notifyIcon.ContextMenu = contextMenu and you're set.
On WPF we can not set the ContextMenu that easily because WPF's ContextMenu class is not related to Forms ContextMenu. The alternative I have been pursuing is to handle NotifyIcon's Click event to open the WPF-style ContextMenu.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// This is intended to be a system tray application so we hide the window
this.Visibility = Visibility.Hidden;
// Winform context menu
// The context menu closes when the user clicks anywhere outside the menu
// The user can navigate the menu with the keyboard arrows and close with ESC
var notifyIcon1 = new System.Windows.Forms.NotifyIcon();
var contextMenu = new System.Windows.Forms.ContextMenu();
var menuItem = new System.Windows.Forms.MenuItem();
menuItem.Text = "WinForm Menu Item";
contextMenu.MenuItems.Add(menuItem);
notifyIcon1.ContextMenu = contextMenu;
notifyIcon1.Icon = Properties.Resources.ico;
notifyIcon1.Visible = true;
// WPF context menu
// The user cannot close the menu by clicking outside its bounds
// Does not detect any keyboard input
var notifyIcon2 = new System.Windows.Forms.NotifyIcon();
notifyIcon2.Icon = Properties.Resources.ico;
notifyIcon2.Visible = true;
notifyIcon2.Click += NotifyIcon2_Click;
}
private void NotifyIcon2_Click(object sender, EventArgs e)
{
var contextMenu = new ContextMenu();
var menuItem = new MenuItem();
menuItem.Header = "WPF Menu Item";
contextMenu.Items.Add(menuItem);
contextMenu.IsOpen = true;
}
}
The issue with this approach is that the WPF ContextMenu never gets any hint that the user has navigated away from the menu and should close (Eg, when the user clicks outside the bounds of the menu). None of the Focus or MouseCapture events are ever triggered and I am unable to close the menu other than by clicking on one of its items.
So the question here, to put slightly different, is: how do I properly emulate the NotifyIcon's ContextMenu closing behavior using WPF's ContextMenu?
I faced similar issue. You can try if you want
notifyIcon1.ContextMenuStrip = new Forms.ContextMenuStrip();
notifyIcon1.ContextMenuStrip.Items.Add("YourMenuItem",null, MenuItemEvent);
I hope this will solve the problem.
I am trying to find how I can add items to devExpress PopupMenu. I have tried the following:
manager = new BarManager();
listBoxMenu = new PopupMenu(manager);
listBoxMenu.ItemLinks.Add(manager.Items["Remove item"]);
listBoxMenu.ItemLinks.Add(manager.Items["Clear items"]);
As shown here http://documentation.devexpress.com/#WindowsForms/CustomDocument5472 (at the bottom), but it gives me an error saying the item is not initialized.
What is the proper way to add items? I can't find it anywhere.
Edit, here is how I did it:
//Creates the popup menu to be used for the keywords listbox
manager = new BarManager();
listBoxMenu = new PopupMenu(manager);
item = new BarButtonItem(manager, "Copy");
item2 = new BarButtonItem(manager, "Clear Item");
item3 = new BarButtonItem(manager, "Clear All Items");
listBoxMenu.ItemLinks.Add(item);
listBoxMenu.ItemLinks.Add(item2);
listBoxMenu.ItemLinks.Add(item3);
//Adds the seperator on the second item
item2.Links[0].BeginGroup = true;
manager.ItemClick += manager_ItemClick;
Check this code snippet and implement using the same way.
//create popup and manage objects
private DevExpress.XtraBars.BarManager barManager1;
private DevExpress.XtraBars.PopupMenu buttonContextMenu;
DevExpress.XtraBars.BarButtonItem menuButtonExport = new DevExpress.XtraBars.BarButtonItem();
DevExpress.XtraBars.BarButtonItem menuButtonSave = new DevExpress.XtraBars.BarButtonItem();
public TestForm8()
{
InitializeComponent();
barManager1 = new BarManager();
this.barManager1.Form = this;
buttonContextMenu = new DevExpress.XtraBars.PopupMenu(barManager1);
this.buttonContextMenu.Name = "subViewContextMenu";
menuButtonExport.Caption = "E&xport";
menuButtonExport.Id = 1;
menuButtonExport.Name = "menuButtonExport";
menuButtonExport.ItemClick += new ItemClickEventHandler(menuButtonExport_ItemClick);
menuButtonSave.Caption = "S&ave";
menuButtonSave.Id = 2;
menuButtonSave.Name = "menuButtonSave";
menuButtonSave.ItemClick += new ItemClickEventHandler(menuButtonSave_ItemClick);
//add items to barmanager
this.barManager1.Items.AddRange(new DevExpress.XtraBars.BarItem[] {
menuButtonExport,
menuButtonSave
});
//create links between bar items and popup
buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonExport"]);
buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonSave"]);
//finally set the context menu to the control or use the showpopup method on right click of control
barManager1.SetPopupContextMenu(btnInsert, buttonContextMenu);
}
Ref by step to include popup:
How to: Create a popup menu
How to: Add items to a container bar item (menu)
Populating Popup Menus
BarManager.SetPopupContextMenu Method
Your manager is empty:
manager = new BarManager();
The example you linked to is using a BarManager that was already created: barManager1, which I assume was created in the designer and populated with items.
From their BarManager help page:
After a BarManager has been added to a form/user control, you can create bars and bar commands using context menus right on the form, using the bar manager's Customization Window or its Designer. Please see the Toolbars Customization section, to learn more.
How to create several MenuItems and add it to ContextMenu in windows phone 7 with C#?
I wrote this sample:
MenuItem q = new MenuItem();
q.Header = "something";
ContextMenu cM = new ContextMenu(q);
and in MSDN there's something about it, but it doesn't compile, because of construct of ContextMenu.
You can add menu items as follows:
ContextMenu cm = new ContextMenu();
cm.Items.Add( new MenuItem() {
Header = "Item 1",
} );
The ContextMenu isn't part of Wp7 natively. You need to look at the Silverlight Toolkit for WP7.
http://silverlight.codeplex.com/
I'm making a WPF application, but in my code I need to make a ContextMenu, it seemed really easy:
_menu = new ContextMenu();
_menu.Items.Add("My menu item");
Then I used it, and everything works like a charm.
However, I need to know when "My menu item" is clicked, but I can't seem to find the right event, I'm searching for something like ItemClick event, but cant't find it...
Try adding an item that is clickable rather than just a string. For example:
_menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Click += MyClickHandler;
item.Header = "My Menu Item";
_menu.Items.Add(item);
I never did it in code, always used XAML.
However, it is something like this:
_menu = new ContextMenu();
MenuItem mi = new MenuItem();
mi.Items.Add("My menu item");
mi.Click += (sender,args) =>
{
// Do what you want, or instead of a lambda
// you can even add a separate method to the class
};
_menu.Items.Add(mi);
The only doubt is adding the text to the menu item. You'll have to try as in the example or maybe add a TextBlock to the MenuItem.Items collection
I think you want something like this:
_menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Header = "My menu item";
item.Click += new RoutedEventHandler(item_Click);
_menu.Items.Add(item);