Reputation: 2790
I have a ToolStripButton that is used as a radio button. When it is checked, a blue outline surrounds the button, but there is no background color. It is not clear enough for the user that the button is checked, so I would like to change the background color to make the check state more visible.
How do I go about changing the highlight color when the Checked property is set to true?
Here is a code snippet:
this.hideInactiveVehiclesToolstripButton.CheckOnClick = true;
this.hideInactiveVehiclesToolstripButton.ForeColor = System.Drawing.Color.Blue;
this.hideInactiveVehiclesToolstripButton.AutoSize = false;
this.hideInactiveVehiclesToolstripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.hideInactiveVehiclesToolstripButton.Image = global::ClientUI.Properties.Resources.toggleInactive;
this.hideInactiveVehiclesToolstripButton.ImageTransparentColor = System.Drawing.Color.Black;
this.hideInactiveVehiclesToolstripButton.Name = "hideInactiveVehiclesToolstripButton";
this.hideInactiveVehiclesToolstripButton.Size = new System.Drawing.Size(48, 48);
this.hideInactiveVehiclesToolstripButton.Text = "Hide Inactive Vehicles";
this.hideInactiveVehiclesToolstripButton.Click +=new System.EventHandler(this.hideInactiveVehiclesToolstripButton_Click);
Upvotes: 23
Views: 22987
Reputation: 121
Try this, it worked for my app, and I had a similar experience trying the suggested workarounds...
yourStatusStripNameHere.Renderer = new CustomToolStripRenderer();
public class CustomToolStripRenderer : ToolStripProfessionalRenderer
{
/// <summary>
/// This will only be called when the ToolStrip is drawn, e.g. TextUpdated, OnLoad, OnRestore, DragOut/DragIn.
/// </summary>
/// <param name="e"><see cref="ToolStripRenderEventArgs"/></param>
protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
{
if (!e.AffectedBounds.IsEmpty)
{
Debug.WriteLine($"[INFO] Painting the toolstrip at {DateTime.Now.ToLongTimeString()}");
Rectangle bounds = new Rectangle(Point.Empty, e.AffectedBounds.Size);
e.Graphics.FillRectangle(Brushes.Navy, bounds);
}
else
base.OnRenderToolStripBackground(e);
}
}
Upvotes: 0
Reputation: 697
Here is the VB.net code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
toolStrip1.Renderer = New MyRenderer()
End Sub
Public Class MyRenderer
Inherits ToolStripProfessionalRenderer
Protected Overrides Sub OnRenderButtonBackground(ByVal e As ToolStripItemRenderEventArgs)
Dim btn As ToolStripButton = e.Item
If (Not IsDBNull(btn) And btn.CheckOnClick And btn.Checked) Then
Dim bounds As Rectangle = New Rectangle(Point.Empty, e.Item.Size)
e.Graphics.FillRectangle(Brushes.Black, bounds)
End If
End Sub
End Class
Upvotes: 0
Reputation: 404
on Event click for every toolStripButton
private void toolStripButton4_Click(object sender, EventArgs e)
{
toolStrip1.Items[0].BackColor = SystemColors.ActiveCaption;
toolStrip1.Items[1].BackColor = SystemColors.Control;
toolStrip1.Items[2].BackColor = SystemColors.Control;
toolStrip1.Items[3].BackColor = SystemColors.Control;
}
Upvotes: 0
Reputation: 942109
You can provide your own tool strip renderer to draw the button's background the way you want them. This example code gives the checked button a very visible black background:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
toolStrip1.Renderer = new MyRenderer();
}
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);
e.Graphics.FillRectangle(Brushes.Black, bounds);
}
else base.OnRenderButtonBackground(e);
}
}
}
Upvotes: 49