none
can I color a particular item in a checkedlistbox? RRS feed

  • Question

  • I want to be able to color  the font or the background of items that match a search.   These items are lines in a 'checkedlistbox' (they also get check marks if they match the search).

    But checkedlistbox.items(iindex) does not seem to have any color property whatsoever.

    Thursday, June 21, 2018 9:29 AM

Answers

  • You will have to handle OnDrawItem to do this e.g.

    I did the above by creating a custom CheckedListBox. It's not perfect, needs work but this is a start.

    Public Class CustomCheckedListBox
        Inherits CheckedListBox
    
        Public Sub New()
            DoubleBuffered = True
        End Sub
    
        Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
    
            If Items.Count = 0 Then
                Exit Sub
            End If
    
            Dim checkSize As Size = CheckBoxRenderer.GetGlyphSize(e.Graphics, VisualStyles.CheckBoxState.MixedNormal)
            Dim dx As Integer = (e.Bounds.Height - checkSize.Width) \ 2
    
            e.DrawBackground()
    
            Dim isChecked As Boolean = GetItemChecked(e.Index)
    
            CheckBoxRenderer.DrawCheckBox(e.Graphics, New Point(dx, e.Bounds.Top + dx),
                                          If _
                                             (isChecked, VisualStyles.CheckBoxState.CheckedNormal,
                                              VisualStyles.CheckBoxState.UncheckedNormal))
    
            Using sf = New StringFormat With {.LineAlignment = StringAlignment.Center}
                Using brush As Brush = New SolidBrush(If(isChecked, CheckedItemColor, ForeColor))
                    e.Graphics.DrawString(Items(e.Index).ToString(), Font, brush,
                                          New Rectangle(e.Bounds.Height, e.Bounds.Top, e.Bounds.Width - e.Bounds.Height,
                                                        e.Bounds.Height), sf)
                End Using
            End Using
        End Sub
        Private _checkedItemColorRenamed As Color = Color.Red
        Public Property CheckedItemColor() As Color
            Get
                Return _checkedItemColorRenamed
            End Get
            Set(ByVal value As Color)
                _checkedItemColorRenamed = value
                Invalidate()
            End Set
        End Property
    End Class
     

    Second version where a List(Of String) is used to perform comparisons against items in the CheckedListBox.

    Public Class CustomCheckedListBox
        Inherits CheckedListBox
        ''' <summary>
        ''' Used to compare items and colorize if located
        ''' </summary>
        ''' <returns></returns>
        Public Property Completionlist As New List(Of String)()
    
        Public Sub New()
            DoubleBuffered = True
        End Sub
        Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
    
            If Items.Count = 0 Then
                Exit Sub
            End If
    
            Dim checkSize As Size = CheckBoxRenderer.GetGlyphSize(e.Graphics, VisualStyles.CheckBoxState.MixedNormal)
            Dim dx As Integer = (e.Bounds.Height - checkSize.Width) \ 2
            e.DrawBackground()
            Dim isChecked As Boolean = GetItemChecked(e.Index)
            CheckBoxRenderer.DrawCheckBox(e.Graphics, New Point(dx, e.Bounds.Top + dx),
                                          If _
                                             (isChecked, VisualStyles.CheckBoxState.CheckedNormal,
                                              VisualStyles.CheckBoxState.UncheckedNormal))
            Dim myFont As Font = e.Font
            Dim myBrush As Brush
            Dim i As Integer = e.Index
    
            If Completionlist.Contains(Items(i).ToString()) Then
                myBrush = Brushes.Red
            Else
                myBrush = Brushes.Black
            End If
    
            e.Graphics.DrawString(Items(i).ToString(), myFont, myBrush,
                                  New Rectangle(e.Bounds.Height, e.Bounds.Top, e.Bounds.Width - e.Bounds.Height,
                                                e.Bounds.Height), StringFormat.GenericDefault)
        End Sub
    
    End Class
    

    Usage

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            CustomCheckedListBox1.Items.Add("Karen")
            CustomCheckedListBox1.Items.Add("Jane")
            CustomCheckedListBox1.Items.Add("Mary")
            CustomCheckedListBox1.Completionlist = New List(Of String) From {"Karen", "Mary"}
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Thursday, June 21, 2018 10:55 AM
    Moderator