Search code examples
c#winformsvisual-studio-2015menustrip

C# Removing Submenu Item Image Margins


See the linked screenshot below.

In short, I need those little white boxes to disappear - they're supposed to house an image, but there is no image, and so I'd rather they disappear.

I've accomplished this using the follow code:

foreach (ToolStripMenuItem menuItem in mnuMain.Items)
   ((ToolStripDropDownMenu)menuItem.DropDown).ShowImageMargin = false;

This works for what I guess are the main items, but not the sub-items, as you can see in the picture.

I've tried a few variations on the above code to try and get it to capture everything instead of just the first level items, but no luck.

What am I doing wrong?

http://i.imgur.com/bst1i4v.png


Solution

  • You should do that for sub items too. To do so, you can use this code:

    private void Form1_Load(object sender, EventArgs e)
    {
        SetValuesOnSubItems(this.menuStrip1.Items.OfType<ToolStripMenuItem>().ToList());
    }
    
    private void SetValuesOnSubItems(List<ToolStripMenuItem> items)
    {
        items.ForEach(item =>
                {
                    var dropdown = (ToolStripDropDownMenu)item.DropDown;
                    if (dropdown != null)
                    {
                        dropdown.ShowImageMargin = false;
                        SetValuesOnSubItems(item.DropDownItems.OfType<ToolStripMenuItem>().ToList());
                    }
                });
    }