Search code examples
c#winformscontextmenustriptoolstripitemtoolstripcombobox

How to change the background color of ToolStripItems?


When I add a ToolStripMenuItem to a ContextMenuStrip and then add a ToolStripComboBox in it, the background color under the ToolStripComboBox isn't blue. However, as you can see it's blue under 'Sub opt2' (which is in the main options of ContextMenuStrip).

My question: How can you change the background that's underneath the ToolStripComboBox?

The Image below is an example of the these two ToolStripComboBox having different background colors:

enter image description here

The Image below is an example of the background color I want to change:

enter image description here


Solution

  • Assume your Sub opt1 ToolStripMenuItem is named options1ToolStripMenuItem:

    you can remove the margin reserved to the Image, casting its DropDown (which is of type ToolStripDropDown) to ToolStripDropDownMenu:

    (options1ToolStripMenuItem.DropDown as ToolStripDropDownMenu).ShowImageMargin = false;
    

    Using the same logic, you can also set the BackColor of the DropDown (not the BackColor of single ToolStripItems. If you change the BackColor of a ToolStripComboBox, as in this case, you just change the property value of the Control itself):

    options1ToolStripMenuItem.DropDown.BackColor = [Your Color];
    

    You can add this code to the Form's Constructor.
    You need to repeat the same operation to configure other sub-menus.