4

OK, someone please tell me why this is not working.

I have a simple MenuStrip in winforms app (c#). It has ToolStripMenuItems.

In the properties window of the designer, I select BackColor = White. In Desginer.cs file I can see it.

Running the app, the background color is Control (grey).

What is going? Why is the backcolor not white?

Thanks

EDIT

This is the code from the Designer.cs:

   this.menuRefresh.BackColor = System.Drawing.Color.White;

Refresh item should be white

EDIT2:

In the code, after loading the form (in the constructor and also in Form_Load event I've placed this:

 menuRefresh.BackColor = Color.White;

Also not helping.

Ehud Grand
  • 3,501
  • 4
  • 33
  • 52

3 Answers3

7

You need to implement a simple renderer class to achieve this. Here is an example:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
      InitializeComponent(); 
      menuStrip1.Renderer = new MyRenderer(); 
    } 

    private class MyRenderer : ToolStripProfessionalRenderer 
    { 
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)      
        { 
            Rectangle rc = new Rectangle(Point.Empty, e.Item.Size); 
            Color c = e.Item.Selected ? Color.Azure : Color.Beige; 
            using (SolidBrush brush = new SolidBrush(c)) 
                e.Graphics.FillRectangle(brush, rc); 
        } 
    } 
} 
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • 1
    @user1223457: So... you are using a custom renderer to start with? Why wasn't this included in your question? – musefan Aug 21 '14 at 12:28
1

The BackColor of the MenuStrip does not determine the background colour of the items included in any tool strip menus (drop downs). These items each have their own BackColor property, which must be set separately.

For example, your "Refresh" item is it's own ToolStripMenuItem, so you need to set the BackColor of that to White also.


In regards to your second edit, setting menuRefresh.BackColor = Color.White; should work fine in either the constructor, or the Form_Load event. I have tested it with both and it works as expected.

musefan
  • 47,875
  • 21
  • 135
  • 185
  • It seems op sets `BackColor` of `ToolStripMenuItem` only. – Sriram Sakthivel Aug 21 '14 at 12:21
  • That's what I did: menuRefresh.BackColor = Color.White; but it's not working – Ehud Grand Aug 21 '14 at 12:21
  • @user1223457: The code you have shown should work fine. Can you try setting it via the designer and see if that has any effect? As it stands, the code you have included is not enough to replicate the issue – musefan Aug 21 '14 at 12:24
0

I wanted to do the same thing to make a contextmenustrip header, where i could set the background, forground and border colors of a toolstripmenuitem.

It is similar to the other answers but a bit more self contained.

How it looks

How the code is implemented;

ContextMenustrip.Items.Add(new CustomCMSItems.ToolStripHeader("Shifts", new Font("Segoe UI", 10, FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 1, false), Color.Black, Color.LightGray, Color.Red));


public class ToolStripHeader : ToolStripMenuItem
{
    Color _BackColor;
    Color _BorderColor;
    Color _FontColor;
    Font _Font;//= new Font("Segoe UI", 10, FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 1, false)

    public ToolStripHeader(string text, Font font, Color textcolor, Color BackgroundColor, Color BorderColor) //: base(new Label())
    {
        this.Padding = Padding.Empty;

        _BackColor = BackgroundColor;
        _BorderColor = BorderColor;
        _FontColor = textcolor;
        _Font = font;

        this.Text = text;
    }

    protected override void OnParentChanged(ToolStrip oldParent, ToolStrip newParent)
    {
        base.OnParentChanged(oldParent, newParent);

        if (newParent != null)
        {
            if (newParent.GetType() == typeof(ContextMenuStrip))
            {
                newParent.Renderer = new HeaderRenderer(_Font, _FontColor, _BackColor, _BorderColor);
            }
        }
    }

    private class HeaderRenderer : ToolStripProfessionalRenderer
    {
        Color _BackColor;
        Color _BorderColor;
        Color _FontColor;
        Font _Font;//= new Font("Segoe UI", 10, FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 1, false)

        public HeaderRenderer(Font font, Color textcolor, Color BackgroundColor, Color BorderColor) //: base(new Label())
        {


            _BackColor = BackgroundColor;
            _BorderColor = BorderColor;
            _FontColor = textcolor;
            _Font = font;

        }

        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            if (e.Item.GetType() == typeof(ToolStripHeader))
            {
                Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);

                SolidBrush brush = new SolidBrush(_BackColor);
                Pen pen = new Pen(_BorderColor);

                e.Graphics.FillRectangle(brush, rc);
                e.Graphics.DrawRectangle(pen, 1, 0, rc.Width - 2, rc.Height - 1);

                return;
            }

            base.OnRenderMenuItemBackground(e);

            if (!e.Item.Selected) base.OnRenderMenuItemBackground(e);
            else
            {
                //Example
                //Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
                //e.Graphics.FillRectangle(Brushes.DarkGray, rc);
                //e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1);

            }
        }

        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            if (e.Item.GetType() == typeof(ToolStripHeader))
            {
                e.TextColor = _FontColor;
                e.TextFont = _Font;
            }
            base.OnRenderItemText(e);
        }

    }
}
Patch
  • 11
  • 3