I have a ToolStrip on my form and am using a custom ToolstripRenderer. In the OnRenderButtonBackground sub, I have it fill a solid (blue in this case) color when a button is clicked. The problem I'm struggling with is, it only works for the first button and no others. I stepped through the code and it appears to be executing the code to fill with the blue solid color, but it never happens visually. The properties for each button are the same, in fact I even tried making an exact copy of the first (working) button and that one did not highlight on click either.
Here is the code I'm using:
Here is the code I'm using:
VB.NET:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
mainstrip.Renderer = New TestRenderer
End Sub
Private Sub toolbarButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFoo.Click, btnBar.Click
Dim item As ToolStripItem
For Each item In Me.mainstrip.Items
If item IsNot sender AndAlso TypeOf item Is ToolStripButton Then
CType(item, ToolStripButton).Checked = False
End If
Next item
End Sub
End Class
Public Class TestRenderer
Inherits System.Windows.Forms.ToolStripRenderer
Protected Overrides Sub OnRenderSeparator(ByVal e As System.Windows.Forms.ToolStripSeparatorRenderEventArgs)
Dim sep As ToolStripSeparator = e.Item
e.Graphics.FillRectangle(Brushes.Silver, 0, 4, 1, sep.Bounds.Height - 8)
End Sub
Protected Overrides Sub OnRenderButtonBackground(ByVal e As System.Windows.Forms.ToolStripItemRenderEventArgs)
Dim button As ToolStripButton = e.Item
If button.Pressed OrElse button.Checked Then
e.Graphics.FillRectangle(New SolidBrush(Color.CornflowerBlue), e.Item.Bounds.X - 10, e.Item.Bounds.Y, e.Item.Bounds.Width, e.Item.Bounds.Height)
Else
e.Graphics.FillRectangle(New SolidBrush(Color.Transparent), e.Item.Bounds.X - 10, e.Item.Bounds.Y, e.Item.Bounds.Width, e.Item.Bounds.Height)
End If
End Sub
Protected Overrides Sub OnRenderToolStripBackground(ByVal e As ToolStripRenderEventArgs)
e.Graphics.FillRectangle(New SolidBrush(Color.Transparent), e.AffectedBounds)
End Sub
End Class