Please consider my ondrawitem methods of my derived checkedListBox class. At this stage I put "-->" in front of he selected item and " " in front of the others. I would like to replace that with a checked box (square) and an empty box respectively, very much as in the oroginal control.

    Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
        MyBase.OnDrawItem(e)
        Dim rectangle As RectangleF = e.Graphics.ClipBounds
        Using brushBackground As New SolidBrush(Me.BackColor)
            e.Graphics.FillRectangle(brushBackground, rectangle)
        End Using
        ' e.DrawBackground()
        'If e.State = DrawItemState.Focus Then
        '    e.DrawFocusRectangle()
        'End If
        Dim index = e.Index
        If index < 0 OrElse index >= Items.Count Then
            Return
        End If
        rectangle = e.Bounds
        Using brush = New SolidBrush(Color.DarkBlue)
            For i As Integer = 0 To Me.Items.Count - 1
                Dim text As String = "     " + Me.Items(i).ToString
                If i = Me.SelectedIndex Then
                    text = "--> " + Me.Items(i).ToString
                End If
                rectangle.Y = i * rectangle.Height
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit
                e.Graphics.DrawString(text, Me.Font, brush, rectangle)
            Next
        End Using
    End Sub

Instead of drawing the box, another approach would be to load 2 images: one with the box and the other without (or whatever images you decide). Then in the OnDrawItem() just call e.Graphics.DrawImage() method to draw the box image or the no-box-image.

Thanks. You're right. Well I took a slightly different approach, and to be honest, I think yours would have given better result.

   Protected Overrides Sub OnDrawItem(e As DrawItemEventArgs)
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault
        Using Brush = New SolidBrush(Color.DarkBlue)
            Dim h As Integer = e.Bounds.Height
            Dim center As New Point(h / 2, h / 2)
            Dim radius As Integer = 5
            Dim textRectangle = New Rectangle(h, 1, e.Bounds.Width - h, h)
            Dim pen As New System.Drawing.Pen(Brush)
            For i As Integer = 0 To Items.Count - 1
                Dim center_i As New Point(center.X, center.Y + i * h)

                drawcircle(center_i, radius, e.Graphics, Me.BackColor, True)
                drawcircle(center_i, radius, e.Graphics, Me.ForeColor)
                If i = Me.SelectedIndex Then
                    drawcircle(center_i, radius - 2, e.Graphics, Me.ForeColor, True)
                End If
                textRectangle.Y = i * h + (h - Me.FontHeight) / 2
                e.Graphics.DrawString(Items(i), Me.Font, Brush, textRectangle)
            Next
        End Using
    End Sub
    Private Sub drawcircle(p As Point, r As Integer, g As Graphics, col As Color, Optional fill As Boolean = False)
        Dim top As New Point(p.X - r, p.Y - r)
        Dim diameter As Integer = r * 2
        If fill = False Then
            g.DrawEllipse(New Pen(col), New Rectangle(top.X, top.Y, diameter, diameter))
        Else
            g.FillEllipse(New SolidBrush(col), New Rectangle(top.X, top.Y, diameter, diameter))
        End If
    End Sub

Well, drawing on the fly allows to customize instead of a static image.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.