Click here to Skip to main content
15,889,865 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm working on winForms C# 2.0.
I want to change the backcolor of my menustrip. It looks as simple as writing menustrip1.Backcolor = Color.FromArgb(38, 37, 37); but I'm having two problems:

1) all toolstripmenuitems are still with the control backcolor. (I already set their backcolor one by one but...)

2) Each toolstipSeparator stays with the white backcolor and if I set a backcolor to each separator, nothing happens, it still has the control color.

I tried to set the renderMode to System but that does not fix my problem.
Also I tried making my own renderer but I got the same result.

I saw that contextMenuStrips can be easily backcolor changed and it changes uniformly(items and separators) .

Thanks in advance. CCB
Posted

1 solution

 
Share this answer
 
v2
Comments
ChrisCreateBoss 23-Jul-15 22:40pm    
Thanks for your help, I'll try and then say how it worked .
Sergey Alexandrovich Kryukov 24-Jul-15 0:15am    
You are welcome. Please don't forget to accept the answer formally.
—SA
ChrisCreateBoss 24-Jul-15 14:07pm    
Ok, tried making foreach loops to go through the items, but when I run the app I get an error saying that ToolStripItem cannot be converted into ToolStripSeparator. I do not know if I am missing another loop or if I wrote the code.incorrectly. Note: my menustrip has 2 dropdownItems before the first separaator, so my code sets correctly the colors to the items but it does not set it right to my separators.
Sergey Alexandrovich Kryukov 24-Jul-15 15:48pm    
Why would you convert them? Some items are separators and some are not. Could you show the fragment of the code you tried?

But the solution is simple. Look: https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripseparator%28v=vs.110%29.aspx.

As you can see, System.Windows.Forms.ToolStripSeparator is System.Windows.Forms.ToolStripItem. And System.Windows.Forms.ToolStripItem is the compile-time element type of the collection. So, the BackColor is the same property for these too classes. Therefore, just use System.Windows.Forms.ToolStripItem. Excuse me, do you understand 1) OOP, 2) in particular, inheritance; 3) in particular, compile-time vs runtime types? 4) in particular, polymorphism? You need to understand it all very well, this is where you are confused, but you cannot be permanently confused and still do programming.

—SA
ChrisCreateBoss 24-Jul-15 16:36pm    
I know about the separator class too, but I just did not know how to implement it to my loop because after some tries, each time I debugged I got the same problem (the try of class conversion). Now it is working. Thanks for your help Sergey. And yes, I will accept your solution.

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