Change highlight color of selected Listbox item

rockola1454

New member
Joined
Feb 16, 2008
Messages
2
Programming Experience
5-10
Hi,

I am having considerable difficulty determining how
to change the "highlight" color of the selected item
in a VB.NET Listbox. I would expect it to be a simple
Listbox property change, but I can't find where to modify it
from the default blue, which is very difficult to see
with my color scheme.

Is anybody familiar with how to do this? Thanks!

-Chris
 
You have to draw the items yourself, this is easy to do. Set DrawMode=OwnerDrawFixed and handle the DrawItem event. Example:
VB.NET:
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    e.DrawBackground()
    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
        e.Graphics.FillRectangle(Brushes.LightGreen, e.Bounds)
    End If
    Using b As New SolidBrush(e.ForeColor)
        e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(e.Index)), e.Font, b, e.Bounds)
    End Using
    e.DrawFocusRectangle()
End Sub
 
Worked perfectly! :)

I was actually already drawing the Item manually, but I didn't have the FillRectangle statement in there. Thanks, John!

-Chris
 
Back
Top