Regarding context menu

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alexis4
    New Member
    • Dec 2009
    • 113

    Regarding context menu

    Hi!

    I have a ContextMenuStri p inside a form. This has several items like that:

    I also attached the image at the bottom of the post.

    For some reason, I want to change all items of the context menu simultaneously. So I wrote this peace of code:

    Code:
    int a = 0;
    foreach (ToolStripItem co in contextMenuStrip1.Items)
    {
      co.Text = "Menu" + a.ToString();
      a++;
    }

    But what I get is "engl1" and "engl2" menus changed into "menu1" and "menu2" as expected, but "engl11" and "engl12" stayed as they were and didn't change into "menu3" and "menu4" as I wanted to.

    So how can I have access to those subitems too?

    Thanks!
    Attached Files
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Instead of using the ToolStripItem object, use a ToolStripMenuIt em object. This has a property called DropDownItems that provides access to the list of sub items. ToolStripItem is a more generic object than ToolStripMenuIt em so it doesn't provide as much information. You can follow the inheritance tree by right-clicking on the type in visual studio and selecting Go To Definition. Keep doing this and you will eventually find ToolStripItem :)

    You may also want to consider a recursive method to handle processing this, as you could conceivably have a menu N levels deep. This should be easy enough to do but if you need help, please feel free to ask!

    Comment

    • alexis4
      New Member
      • Dec 2009
      • 113

      #3
      Thank you GaryTexmo, I will work on that and let you know.

      Comment

      Working...