CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2014
    Posts
    13

    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;
        }
    {

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to loop through top level contextmenustrip dropdown items?

    In the foreach loop, change cms.Items[0] to cms.Items

  3. #3
    Join Date
    Aug 2014
    Posts
    13

    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;

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to loop through top level contextmenustrip dropdown items?

    Code:
    foreach (var item in cms.Items[0].DropDownItems)

  5. #5
    Join Date
    Aug 2014
    Posts
    13

    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?)

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to loop through top level contextmenustrip dropdown items?

    Quote Originally Posted by rfresh737 View Post
    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)
    {
                        
    }

  7. #7
    Join Date
    Aug 2014
    Posts
    13

    Re: How to loop through top level contextmenustrip dropdown items?

    Got it...thank you...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured