In Microsoft Visual Basic 2010, a listbox has a forcolor and backcolor property. These properties change the forcolour and backcolor for all the items in the listbox. By default there is no property for the forecolor and backcolor of an individual item on a listbox, I am aware there is on a list view but I would still wish to use a listbox. I am trying to have the ability to change the forecolor and backcolor properties of individual items in the listbox. To do this the listbox's draw item sub must be used with the listbox's drawmode property set to OwnerDrawFixed. Then using a brush colour along with the e.graphics the forecolor or backcolor can be changed. I have seen and followed examples of how to do this for the currently selected item. Such as the one from ehow's website. What I am tiring to do however is change the colour of the litsbox item as it is added depending on a variable.
Here is my code:
Expand|Select|Wrap|Line Numbers
- Private Sub listbox_add()
- Me.ListBox1.Items.Add(listbox_text(list_num)) ' adds the line to the list box
- add_item_colour = True
- ListBox1.Refresh()
- End Sub
- Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
- Dim myBrush As Brush = Brushes.Black
- e.DrawBackground()
- If add_item_colour = True Then
- If blue_message = True Then
- myBrush = Brushes.Blue
- Else
- myBrush = Brushes.Black
- End If
- e.Graphics.DrawString(ListBox1.Items.Item(list_num), ListBox1.Font, myBrush, _
- New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
- add_item_colour = False
- End If
- e.DrawFocusRectangle()
- End Sub
The problem I seem to be having is that Listbox1.Refresh() command does not seem to be triggering the ListBox1_DrawItem sub every time it is called. I found this by using brake points. Does anyone know why this might be the case and how I could fix it?
Thanks, any help on this would be much appreciated.