Problem changing toolstrip button backcolor on click using toolstriprenderer

mscdex

Member
Joined
Feb 21, 2007
Messages
5
Programming Experience
5-10
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:

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
 
e.Item.Bounds is rectangle relative to ToolStrip, but drawing is relative to the item, so you can adjust the rectangle location to (0,0):
VB.NET:
Dim rct As Rectangle = e.Item.Bounds
rct.Location = New Point(0, 0)
Using b As New SolidBrush(Color.CornflowerBlue)
    e.Graphics.FillRectangle(b, rct)
End Using
You can also use the e.Item.ContentRectangle, it excludes border and is rectangle relative to the item.
 
Wow, talk about a brain fart. I totally forgot about ContentRectangle (it's been awhile since I've worked with custom renderers). Thanks for the heads up! :)
 
Lower and Right Side of Rectangle not drawn...

sorry to reuse your code guys, but it seems the rectangle is not fully drawn once the mouse hovers over any of the buttons... black line is drawn only at the top and left side of the button ...

any suggestion?

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.Selected Then

Dim rct As Rectangle = e.Item.Bounds
rct.Location = New Point(0, 0)
Using b As New SolidBrush(Color.Transparent)
e.Graphics.FillRectangle(b, rct)
e.Graphics.DrawRectangle(Pens.Black, rct) ' <<< -can't fully draw or enclose the button in a rectangle on highlight or mouseover...

End Using
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


btw, im not a skilled vb.net user... i make my projects through code reuse if not referencing of samples all over the net... my apologies for using your codes... current project is: check in counter program for helicopter passengers...... though not for profit, just to help me go through my job with ease... more so, to impress my boss :eek::eek:
 
Last edited:
sorry to bother, i found out that the size of my toolstrip is the same as with the rct width and height... my bad...

Protected Overrides Sub OnRenderButtonBackground(ByVal e As System.Windows.Forms.ToolStripItemRenderEventArgs)

Dim button As ToolStripButton = e.Item
If button.Selected Then
Dim rct As Rectangle = e.Item.Bounds
rct.Width -= 2 '>>> solved the issue
rct.Height -= 2
rct.Location = New Point(0, 0)
Using b As New SolidBrush(Color.Transparent)
e.Graphics.FillRectangle(b, rct)
End Using
e.Graphics.DrawRectangle(Pens.Black, rct)
End If

End Sub
 
Back
Top