-
September 14th, 2014, 03:19 PM
#1
How to loop through top level contextmenustrip dropdown items?
I need to loop through all the top level contextmenustrip dropdown items.
So, I set cms.Items[0] but this gives an error with the foreach loop: "foreach cannot operate on variables of type toolstripitem".
How can I loop through just the cms.Items[0] top level (dropdown) items?
Thanks...
Code:
ContextMenuStrip cms = (ContextMenuStrip)sender;
foreach (ToolStripMenuItem item in cms.Items[0])
{
foreach (ToolStripMenuItem dditem in item.DropDownItems)
{
dditem.Checked = true;
}
{
-
September 14th, 2014, 11:10 PM
#2
Re: How to loop through top level contextmenustrip dropdown items?
In the foreach loop, change cms.Items[0] to cms.Items
-
September 14th, 2014, 11:38 PM
#3
Re: How to loop through top level contextmenustrip dropdown items?
I have that. It loops through ALL the top level cms menu items.
cms.Items[0] is the "Font Size" menu item with it's dropdown items
cms.Items[1] is the "Expander Vertical Size" menu item with it's dropdown items.
I thought there might be a way to foreach through just the cms.Items[0] part rather than loop through ALL of them.
Here is more of my menu code
Code:
cmsListBox = new ContextMenuStrip();
cmsListBox.Items.Clear();
cmsListBox.Opening += new System.ComponentModel.CancelEventHandler(cmsListBox_Opening);
listbox_font_size = new ToolStripMenuItem("Font Size");
cmsListBox.Items.Add(listbox_font_size);
listbox_font_size.DropDownItems.Add("8");
listbox_font_size.DropDownItems.Add("9");
listbox_font_size.DropDownItems.Add("10");
listbox_font_size.DropDownItems.Add("11");
listbox_font_size.DropDownItems.Add("12");
listbox_font_size.DropDownItems.Add("13");
listbox_font_size.DropDownItems.Add("14");
listbox_font_size.DropDownItemClicked += new ToolStripItemClickedEventHandler(Listbox_Font_Size_Menu_ItemClicked);
listbox_font_size.Name = "FontSize";
ToolStripMenuItem listbox_expander_bar_vertical_size = new ToolStripMenuItem("Expander Vertical Size");
cmsListBox.Items.Add(listbox_expander_bar_vertical_size);
listbox_expander_bar_vertical_size.DropDownItems.Add("200");
listbox_expander_bar_vertical_size.DropDownItems.Add("300");
listbox_expander_bar_vertical_size.DropDownItems.Add("400");
listbox_expander_bar_vertical_size.DropDownItems.Add("500");
listbox_expander_bar_vertical_size.DropDownItems.Add("600");
listbox_expander_bar_vertical_size.DropDownItemClicked += new ToolStripItemClickedEventHandler(Listbox_Expander_Bar_Vertical_Size_Menu_ItemClicked);
listbox_expander_bar_vertical_size.Name = "ExpanderVerticalSize";
listBoxTopLevelChecklist.ContextMenuStrip = cmsListBox;
-
September 15th, 2014, 12:01 PM
#4
Re: How to loop through top level contextmenustrip dropdown items?
Code:
foreach (var item in cms.Items[0].DropDownItems)
-
September 15th, 2014, 12:03 PM
#5
Re: How to loop through top level contextmenustrip dropdown items?
That gives me an error:
'System.Windows.Forms.ToolStripItem' does not contain a definition for 'DropDownItems' and no extension method 'DropDownItems' accepting a first argument of type 'System.Windows.Forms.ToolStripItem' could be found (are you missing a using directive or an assembly reference?)
-
September 15th, 2014, 01:12 PM
#6
Re: How to loop through top level contextmenustrip dropdown items?
Originally Posted by rfresh737
That gives me an error:
'System.Windows.Forms.ToolStripItem' does not contain a definition for 'DropDownItems' and no extension method 'DropDownItems' accepting a first argument of type 'System.Windows.Forms.ToolStripItem' could be found (are you missing a using directive or an assembly reference?)
Since the menu strip is a container that takes different types of items, you need to cast Items[0] to ToolStripMenuItem to access the DropDownItems list.
Code:
foreach (var item in ((ToolStripMenuItem)menuStrip1.Items[0]).DropDownItems)
{
}
-
September 15th, 2014, 01:20 PM
#7
Re: How to loop through top level contextmenustrip dropdown items?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|