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);
Related
I am using the ThriveGmbH.BottomNavigationBar.XF Nuget package to add a bottom tab bar to my application
BottomBarPage bottomBar = new BottomBarPage
{
};
var tab1 = new MainPage();
var tab2 = new ReceivePage(null);
var tab3 = new SendPage(false);
var tab4 = new SendPage(false);
var tab5 = new InfoPage(null);
bottomBar.Children.Add(tab1);
bottomBar.Children.Add(tab2);
bottomBar.Children.Add(tab3);
bottomBar.Children.Add(tab4);
bottomBar.Children.Add(tab5);
How do I add a listener to this BottomBarPage which checks which of the tabs is currently selected, so that I can add the code below to this listener.
if (bottomBar.SelectedItem == bottomBar.Children[3])
{
//do something
}
Use Android.Support.Design.Widget.TabLayout to create a tabLayout object and create Tab Item elements nested inside a Tab Layout element in your XML.
You can use a TabSelected event on this object like so:
tabLayout.TabSelected += OnTabSelected
Then you can write your OnTabSelected code.
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);
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.
I have a form which contains tab panel with many tap pages. Each of them has its own context menu (display on right-click). But If I add a ToolStripMenuItem to multiple ContextMenuStrips only last menu strip really has this menu item.
Simple code example is:
ToolStripMenuItem tim_refresh = new ToolStripMenuItem("Refresh", null, (o, e) =>
{
MessageBox.Show("Refresh");
});
ContextMenuStrip cms1 = new ContextMenuStrip();
cms1.Items.Add(tim_refresh);
ContextMenuStrip cms2 = new ContextMenuStrip();
cms2.Items.Add(tim_refresh);
this.tblDataManagerObjects.ContextMenuStrip = cms1;
this.tblDataSourceTypes.ContextMenuStrip = cms2;
If one shows this menus one by one, first will be empty...How can I achieve what I want?
Thi is because visual can not be child of multiple diferent visuals in the same time. In your case tim_refresh is a child of cms1 and cms2 at the same time.
You need to create two completely separate instances of ToolStripMenuItem.
EDIT:
You can extract visual creation in factor method to simplify multiple objects instantiation:
private ToolStripMenuItem CreateToolStripMenuItem(string name)
{
return new ToolStripMenuItem(name, null, (o, e) =>
{
MessageBox.Show(name);
});
}
// then just call it once per each menu strip
ContextMenuStrip cms1 = new ContextMenuStrip();
cms1.Items.Add(CreateToolStripMenuItem("Refresh"));
ContextMenuStrip cms2 = new ContextMenuStrip();
cms2.Items.Add(CreateToolStripMenuItem("Refresh"));
one context menu is displayed once at a time; you probably don't need many clones everywhere, but you may want to move one single instance of your menu items to the menu menu strip at the moment menu strip is opening;
I'm moving here the named (grand) parent's items to the child (currently opening) menu when the local copy is empty, and all the next ctx openings I just AddRange, which moves the "located" three menu items from the previously opened ctxMenuStrip to the currently-opening-one:
// http://stackoverflow.com/questions/8307959/toolstripmenuitem-for-multiple-contextmenustrip?rq=1
// http://stackoverflow.com/questions/6275120/toolstripmenuitem-added-to-several-places?rq=1
// WILL_ADD_PARENT_MENU_ITEMS_IN_Opening first time opened we locate common menu items from GrandParent, then we move them to the current slider; cool?
private static ToolStripItem[] separatorLoadSave = null;
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) {
if (separatorLoadSave == null) {
separatorLoadSave = new ToolStripItem[3];
Control slidersAutoGrow = base.Parent;
if (base.Parent.Name != "SlidersAutoGrow") return;
Control slidersForm = slidersAutoGrow.Parent;
if (slidersForm.Name != "SlidersForm") return;
ToolStripItem[] separator = slidersForm.ContextMenuStrip.Items.Find("toolStripSeparator1", false);
if (separator.Length > 0) separatorLoadSave[0] = separator[0];
ToolStripItem[] load = slidersForm.ContextMenuStrip.Items.Find("mniParameterSetLoad", false);
if (load.Length > 0) separatorLoadSave[1] = load[0];
ToolStripItem[] save = slidersForm.ContextMenuStrip.Items.Find("mniParameterSetSave", false);
if (save.Length > 0) separatorLoadSave[2] = save[0];
}
this.contextMenuStrip1.SuspendLayout();
this.contextMenuStrip1.Items.AddRange(separatorLoadSave);
this.contextMenuStrip1.ResumeLayout();
}
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);