Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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...

C#
            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("10");
            listbox_font_size.DropDownItems.Add("12");
            listbox_font_size.DropDownItemClicked += new ToolStripItemClickedEventHandler(Listbox_Font_Size_Menu_ItemClicked);

            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("400");
            listbox_expander_bar_vertical_size.DropDownItemClicked += new ToolStripItemClickedEventHandler(Listbox_Expander_Bar_Vertical_Size_Menu_ItemClicked);

            listBoxTopLevelChecklist.ContextMenuStrip = cmsListBox;


ContextMenuStrip cms = (ContextMenuStrip)sender;

foreach (ToolStripMenuItem item in cms.Items[0])
{
    foreach (ToolStripMenuItem dditem in item.DropDownItems)
    {
        dditem.Checked = true;
    }
{
Posted
Updated 14-Sep-14 17:04pm
v2
Comments
[no name] 14-Sep-14 17:55pm    
cms.Items[0] is one element of an array. What would you expect to iterate over?
Sergey Alexandrovich Kryukov 14-Sep-14 21:19pm    
Are you talking about System.Windows.Forms.ContextMenuStrip? Always specify types by their full names. There are too many similarly named types in different libraries.
—SA
rfresh 14-Sep-14 23:00pm    
Yes...sorry...
rfresh 14-Sep-14 23:05pm    
I have updated with more of my menu code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900