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
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.
--
Happy coding!
Morten Wennevik [C# MVP]
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
///
--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"pothik05" <poth...@discussions.microsoft.com> wrote in message
news:1E4F5C84-AD21-4C3F...@microsoft.com...