Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27349

Change a check box's tick color

$
0
0
Hey all,
I am changing the color of a checkbox tick square by creating a custom checkbox and painting over the box, and if the check state is checked, I then paint the check. The code below works but it seems a bit gludgy and I am wondering if there may be a better way.
vb Code:
  1. Public Class myCheckBox
  2.  
  3.     Inherits CheckBox
  4.  
  5. #Region "Properties"
  6.     Private _tickColor As Color = Color.Yellow
  7.     Public Property tickColor() As Color
  8.         Get
  9.             Return _tickColor
  10.         End Get
  11.         Set(ByVal value As Color)
  12.  
  13.             _tickColor = value
  14.  
  15.         End Set
  16.     End Property
  17. #End Region
  18.  
  19.  
  20.     Private Sub myCheckBox_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  21.  
  22.         Const BOX_LEFT As Integer = 1
  23.         Const BOX_SIZE As Integer = 11
  24.  
  25.         Dim boxTop As Integer = CInt(Me.Height / 2) - CInt(BOX_SIZE / 2)
  26.         Dim r As New Rectangle(BOX_LEFT, boxTop, BOX_SIZE, BOX_SIZE)
  27.  
  28.         Dim br As New SolidBrush(_tickColor)
  29.         e.Graphics.FillRectangle(br, r)
  30.  
  31.         If Me.Checked Then
  32.             Const CHECK_TEXT As String = "" 'checkmark from the Wingdings font
  33.             e.Graphics.DrawString(CHECK_TEXT, New Font("wingdings", Me.Font.Size), Brushes.Black, New Point(1, boxTop))
  34.         End If
  35.     End Sub
  36. End Class
The problem I'm having is finding the location of the tick box. If the checkbox.AutoSize=False and the user changes the height of the checkbox, sometimes the code I have fits the drawn rectangle perfectly, but sometime it's a pixel off and looks bad. Whether it fix or not depends on the height of the box and discrepancy is mostly likely because of rounding issues in math.

I am wondering if there is some other method to find the tick box directly? I know I can create an entirely new checkbox using a control, a label and the CheckBoxRenderer Class but that is a bit more work.
Any ideas?
thanks
kevin

Viewing all articles
Browse latest Browse all 27349

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>