How to change toolstripbutton's backcolor property when clicked on it or checked?
We need to have a ToolStripLabel or ToolStripButton and its back color changed during runtime. It just won't change its BackColor.
This is affected by the ToolStrip's RenderMode setting. When you change it in System, the BackColor property will have an effect.
Make it look similar to below:
public partial class Form1 : Form {
public Form1()
{
InitializeComponent();
this.toolStrip1.Renderer = new MyRenderer();
}
private class MyRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderLabelBackground(ToolStripItemRenderEventArgs e)
{
using (var brush = new SolidBrush(e.Item.BackColor))
{
e.Graphics.FillRectangle(brush, new Rectangle(Point.Empty, e.Item.Size));
}
}
}
private void ToolStripButton1_Click(object sender, EventArgs e)
{
if (ToolStripButton1.Checked)
ToolStripButton1.BackColor = Color.SkyBlue;
else
ToolStripButton1.BackColor = SystemColors.Control;
}
}