Click here to Skip to main content
15,846,144 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I change the Highlight colour of a Listbox from the default Blue colour to any colour?
phil.o 17-Jun-14 13:49pm    
WPF? Windows Forms? VB.NET is just a language and does not presume anything about what engine you plan to use.
Please tag your question according to the field you're interested in.
vbGoof 18-Jun-14 12:15pm    
Windows Forms
vbGoof 18-Jun-14 9:54am    
With the listbox [drawmode] property set to [OwnerDrawFixed] this code works if items are added at design time. My listbox is generated with data from command prompt

'Change Highlight colour
Private Sub ListBox2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox2.DrawItem
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.Graphics.FillRectangle(Brushes.Gray, e.Bounds)
End If
Using b As New SolidBrush(e.ForeColor)
e.Graphics.DrawString(ListBox2.GetItemText(ListBox2.Items(e.Index)), e.Font, b, e.Bounds)

End Using

End Sub
phil.o 18-Jun-14 13:11pm    
Better improve your question with your code rather than putting it in comments; comments are not really meant to display lines of code.
While in the question, you can enclose your code between <pre> tags, which will take care of the correct formatting.

What happens when items are added at runtime ?
vbGoof 20-Jun-14 11:07am    
The information does not appear but when I scroll in the listbox, or when i do a ListCount it indicates that there are items in there but they are not visible. I have tried <pre> .toString </pre> but nothing appears. I managed to change the highlight colour. When the listbox is set to [DrawMode:Normal] the data is there

There is no direct property to do that. You have to owner draw the ListView to do that,
You can find it here
Share this answer
<pre lang="vb">Private Sub ListView1_ItemMouseHover(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemMouseHoverEventArgs) Handles ListView1.ItemMouseHover
        If previousListViewItem IsNot Nothing Then
            previousListViewItem.ForeColor = Nothing
            previousListViewItem.BackColor = Nothing
        End If

        e.Item.ForeColor = Color.Red &#39;Color.FromKnownColor(KnownColor.HighlightText)
        e.Item.BackColor = Color.Gray &#39;Color.FromKnownColor(KnownColor.Highlight)

        previousListViewItem = e.Item
    End Sub</pre>
Share this answer
The code is from here
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900