4.6
(7)

How to Highlight or change the background color of checked ToolStripButton in windows form c#?

To Highlight or change the background color of checked/current ToolStripButton in Windows Form C# you need to implement custom render class that inherits ToolStripProfessionalRenderer class.

Here is the code
Create custom class

private class MyRenderer : ToolStripProfessionalRenderer
        {
            protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
            {
                var btn = e.Item as ToolStripButton;
                if (btn != null && btn.CheckOnClick && btn.Checked)
                {
                    Rectangle bounds = new Rectangle(Point.Empty, e.Item.Size);
                     Color c = Color.FromArgb(0, 147, 230); //Your custom color here
                    var brush = new SolidBrush(c);
                    e.Graphics.FillRectangle(brush, bounds);
                }
                else base.OnRenderButtonBackground(e);
            }
        }

Assign the custom class to renderer property for the ToolStrip control.

//  It assigns the Renderer property for the ToolStrip control.
 public frmMain()
        {
            InitializeComponent();
  //tsSelector is your toolstripselector
            tsSelector.Renderer = new MyRenderer();
        }

If you want to highlight or change the background of current checked ToolStripButton only then you need to uncheck all others ToolStripButton
Here is the code to uncheck all others ToolStripButton

private void ClearAllSelectedButton(object sender)
        {
            foreach (ToolStripButton item in ((ToolStripButton)sender).GetCurrentParent().Items)
            {
                if (item == sender) item.Checked = true;
                if ((item != null) && (item != sender))
                {
                    item.Checked = false;
                }
            }
        }

Call the ClearAllSelectedButton from ToolStripButton  click event.

Hope this will helps you.

How useful was this post?

Click on a star to rate it!

Average rating 4.6 / 5. Vote count: 7

As you found this post useful...

Share this post on social media!

We are sorry that this post was not useful for you!

Let us improve this post!