DrawItem event of ListBox and CheckedListBox control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I can not make the DrawItem event to work on the CheckedListBox control. I
suggestion will be highly appreciated.

I have the following code. The code works for ListBox and the second item
(Two) is displayed in red in the ListBox but the CheckedLIstBox displays
everything in black (where it should be displayed as red).

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
CheckedListBox1.DrawMode = DrawMode.OwnerDrawFixed
AddHandler CheckedListBox1.DrawItem, AddressOf
Me.CheckedListBox1_DrawItem

ListBox1.DrawMode = DrawMode.OwnerDrawFixed
AddHandler ListBox1.DrawItem, AddressOf Me.ListBox1_DrawItem


End Sub

Sub CheckedListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs)
e.DrawBackground()
Dim textBrush As Brush = SystemBrushes.ControlText
Dim drawFont As Font = e.Font
If (CheckedListBox1.Items.Item(e.Index).ToString() = "Two") Then
textBrush = Brushes.Red
drawFont = New Font(drawFont.FontFamily, drawFont.Size,
FontStyle.Bold)
ElseIf (e.State And DrawItemState.Selected) > 0 Then
textBrush = SystemBrushes.HighlightText
End If

e.Graphics.DrawString(CheckedListBox1.Items.Item(e.Index).ToString(),
drawFont, textBrush, e.Bounds.X, e.Bounds.Y)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
CheckedListBox1.Items.Add("One")
CheckedListBox1.Items.Add("Two")
CheckedListBox1.Items.Add("Three")
CheckedListBox1.Items.Add("Four")
ListBox1.Items.Add("One")
ListBox1.Items.Add("Two")
ListBox1.Items.Add("Three")
ListBox1.Items.Add("Four")
End Sub

Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs)
e.DrawBackground()
Dim textBrush As Brush = SystemBrushes.ControlText
Dim drawFont As Font = e.Font
If (ListBox1.Items.Item(e.Index).ToString() = "Two") Then
textBrush = Brushes.Red
drawFont = New Font(drawFont.FontFamily, drawFont.Size,
FontStyle.Bold)
ElseIf (e.State And DrawItemState.Selected) > 0 Then
textBrush = SystemBrushes.HighlightText
End If
e.Graphics.DrawString(ListBox1.Items.Item(e.Index).ToString(),
drawFont, textBrush, e.Bounds.X, e.Bounds.Y)
End Sub
 
HI pothik05,

I'm afraid custom drawing is not supported for CheckedListBox. You can however 'cheat' and make a regular ListBox look like a CheckedListBox. The DrawMode/DrawItem in CheckedListBox is used internally only.
 
CheckedListbox's OnDrawItem does not raise the DrawItem event so you can't
do it like that. That's why the DrawMode property has been hidden from the
property browser.

You can however, Inherit from CheckedListBox and override the OnDrawItem
method. This saves you having to implement the checked property of the
items, which you would need to do if you inherit from ListBox or else just
custom paint Listbox.

with slight modification for checkmarks, your code would then be:
\\\
Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)

e.DrawBackground()

Dim textBrush As Brush = SystemBrushes.ControlText
Dim drawFont As Font = e.Font

If (Me.Items.Item(e.Index).ToString() = "Two") Then
textBrush = Brushes.Red
drawFont = New Font(drawFont.FontFamily, drawFont.Size, _
FontStyle.Bold)
ElseIf (e.State And DrawItemState.Selected) > 0 Then
textBrush = SystemBrushes.HighlightText
End If

Dim checkRect As New Rectangle(e.Bounds.X, e.Bounds.Y, _
e.Bounds.Height, e.Bounds.Height)

e.Graphics.FillRectangle(SystemBrushes.Window, checkRect)

If Me.CheckedIndices.Contains(e.Index) Then
ControlPaint.DrawMenuGlyph(e.Graphics, checkRect, _
MenuGlyph.Checkmark)
End If

e.Graphics.DrawString(Me.Items.Item(e.Index).ToString(), drawFont, _
textBrush, checkRect.Right, e.Bounds.Y)

End Sub
///
 
Back
Top