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.
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 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've a tabcontrol on my page with 2 tabs. Now I want to create another tabcontrol dynamically and want to add the existing tab control to dynamically created tab control tabs.
Is is possible? I'm not able to add this.
Here is my code:
TabControl tbdynamic = new TabControl();
TabPage tbpaagedynamic = new TabPage();
tbpaagedynamic.Controls.Add(statictabcontrol);
tbdynamic.TabPages.Add(tbpaagedynamic);
Any idea?
Yes, it is posiible.
Add dynamic tab to Form :
this.Controls.Add(tbdynamic);
example
TabControl tbdynamic = new TabControl();
tbdynamic.Height = 200;
tbdynamic.Width = 200;
TabPage mPage = new TabPage();
mPage.Text = "Test Page";
tbdynamic.TabPages.Add(mPage);
mPage.Controls.Add(statictabcontrol);
statictabcontrol.Top = 0;
statictabcontrol.Left = 0;
this.Controls.Add(tbdynamic);
Just add the bringToFrontMethod() at the end of adding it to your Window.
tbdynamic.BringToFRont();
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 have a form. I've added the strip down button using drag and drop in the form. How can I (in the program) create and fill the toolStripMenu Item? My menu could contain different element...with different names.
If you want to add items programmatically to a ToolStripDropDownButton just do:
var item1 = new ToolStripButton("my button");
toolStripDropDownButton1.DropDownItems.Add(item1);
var item2 = new ToolStripComboBox("my combo");
toolStripDropDownButton1.DropDownItems.Add(item2);
// etc ...
Instead, if you need to add other ToolStripDropDownButton or other elements directly to you menu (ToolStrip), just do:
var item1 = new ToolStripDropDownButton("my dropdown button");
toolStrip1.Items.Add(item1);
var item2 = new ToolStripProgressBar("my progress bar");
toolStrip1.Items.Add(item2);
// etc ...
EDIT:
You must do it after InitializeComponent() otherwise you won't be able to access to design-time added components, e.g.:
InitializeComponent();
// we're after InitializeComponent...
// let's add 10 buttons under "toolStripDropDownButton1" ...
for (int i = 0; i < 10; i++)
{
var item = new ToolStripButton("Button_"+i);
toolStripDropDownButton1.DropDownItems.Add(item);
}
For the DropDown property you need a ContextMenuStrip. The easiest way to find out how to fill it up is to drag&drop one from the toolbox onto your form, fill it up, select it in the DropDown property and afterwards take a look into the Designer.cs file to see how all the stuff is glued together.
The drawback of using the DropDownItems property is that you can't alter some properties like ShowImageMargin.