Imports System.ComponentModel
Public Class CustomCheckedListBox
Inherits CheckedListBox
Private _CheckedBackColor As Color = Color.Yellow
<Category("Appearance"), Description("The background color of checked items.")> _
Public Property CheckedBackColor() As Color
Get
Return _CheckedBackColor
End Get
Set(ByVal value As Color)
_CheckedBackColor = value
End Set
End Property
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
Dim checked As Boolean = Me.CheckedIndices.Contains(e.Index)
Dim textrectangle As Rectangle = e.Bounds
textrectangle.X += textrectangle.Height
textrectangle.Width -= textrectangle.Height + 1
'back color
If checked Then
Using sb As New SolidBrush(Me._CheckedBackColor)
e.Graphics.FillRectangle(sb, textrectangle)
End Using
Else
Using sb As New SolidBrush(Me.BackColor)
e.Graphics.FillRectangle(sb, textrectangle)
End Using
End If
'checkbox
Dim boxlocation As Point = e.Bounds.Location
boxlocation.Offset(1, 1)
Dim checkstate As VisualStyles.CheckBoxState = VisualStyles.CheckBoxState.UncheckedNormal
If checked Then
checkstate = VisualStyles.CheckBoxState.CheckedNormal
End If
CheckBoxRenderer.DrawCheckBox(e.Graphics, boxlocation, checkstate)
'text
Using sb As New SolidBrush(Me.ForeColor)
Dim text As String = Me.Name
If Me.Items.Count > 0 Then
text = Me.GetItemText(Me.Items(e.Index))
End If
Dim format As New StringFormat
format.LineAlignment = StringAlignment.Center
textrectangle.Inflate(-3, 0)
e.Graphics.DrawString(text, Me.Font, sb, textrectangle, format)
End Using
'focus rectangle
Dim focused As Boolean = (e.State And DrawItemState.Selected) = DrawItemState.Selected
If focused Then
Using p As New Pen(Color.Black)
p.DashStyle = Drawing2D.DashStyle.Dot
textrectangle.Inflate(2, -1)
e.Graphics.DrawRectangle(p, textrectangle)
End Using
End If
End Sub
End Class