Successful Example - Revised 5 Sep 2013 10:57 EDT to include RemoveAt
The following code works.
I tested using Visual Studio 2012 on a Windows 8 PC.
By using the Visual Studio Interactive Debugger, I determined that all visible rows in the
ListBox
are refreshed when a new row is added and that if a new row is not visible, it is not drawn. Therefore, I had to save the brush color for each row in a collection
before adding the new row and use that brush color each time the associated ListBox row is drawn. I use Collection Class to save the brush colors for each row of the ListBox.
The
Refresh
method was not needed.
Dim colColors As Collection = New Collection
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub
Private Sub listbox_add_Blue()
SetColorForNewRow(Brushes.Blue)
ListBox1.Items.Add("Blue")
End Sub
Private Sub listbox_add_Black()
SetColorForNewRow(Brushes.Black)
ListBox1.Items.Add("Black")
End Sub
Sub listbox_remove_0()
If ListBox1.Items.Count < 1 Then Exit Sub
colColors.Remove(1)
ListBox1.Items.RemoveAt(0)
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
If e.Index >= 0 AndAlso e.Index < ListBox1.Items.Count Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _
e.Font, colColors.Item(1 + e.Index), _
e.Bounds, StringFormat.GenericDefault)
End If
e.DrawFocusRectangle()
End Sub
Sub SetColorForNewRow(ByVal MyBrush As Brush)
colColors.Add(MyBrush)
End Sub
Private Sub btnBlack_Click(sender As Object, e As EventArgs) Handles btnBlack.Click
listbox_add_Black()
End Sub
Private Sub btnBlue_Click(sender As Object, e As EventArgs) Handles btnBlue.Click
listbox_add_Blue()
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
listbox_remove_0()
End Sub
_____________________________________________________________________________________________
First attempt using an array to store brush colors (Partial Success)
The following code works for small number of items in the
ListBox
. I tested using Visual Studio 2012 on a Windows 8 PC. I am still studying how to handle the
DrawItem
event when the vertical scroll bar is active.
By using the Visual Studio Interactive Debugger, I determined that all rows in the
ListBox
are refreshed when a new row is added. Therefore, I had to save the brush color for each row in an array and use that brush color each time the associated ListBox row is redrawn. The
Refresh
method was not needed.
This would have been a lot easier with a
ListView
control.
Dim blue_message As Boolean = False
Dim AddMode As Boolean = False
Dim intNewIndex As Integer
Dim arrColors(0 To 100) As Brush
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub
Private Sub listbox_add_Blue()
blue_message = True
AddMode = True
intNewIndex = ListBox1.Items.Count
ListBox1.Items.Add("Blue")
End Sub
Private Sub listbox_add_Black()
blue_message = False
AddMode = True
intNewIndex = ListBox1.Items.Count
ListBox1.Items.Add("Black")
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 AddMode AndAlso e.Index = intNewIndex Then
If blue_message Then
myBrush = Brushes.Blue
End If
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), e.Font, myBrush, _
e.Bounds, StringFormat.GenericDefault)
If e.Index > arrColors.GetUpperBound(0) Then
ReDim Preserve arrColors(0 To arrColors.Count + 100)
End If
arrColors(e.Index) = myBrush
AddMode = False
Else
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _
e.Font, arrColors(e.Index), _
e.Bounds, StringFormat.GenericDefault)
End If
e.DrawFocusRectangle()
End Sub
Private Sub btnBlack_Click(sender As Object, e As EventArgs) Handles btnBlack.Click
listbox_add_Black()
End Sub
Private Sub btnBlue_Click(sender As Object, e As EventArgs) Handles btnBlue.Click
listbox_add_Blue()
End Sub
_____________________________________________________________________________________________
Original Version of the Solution
I looked at the Microsoft Help page for ListBox.DrawItem event and made a few changes to
e.Graphics.DrawString
statement. Also, notice that I moved the location of
add_item_colour = True
.
I did not test.
Private Sub listbox_add()
add_item_colour = True
Me.ListBox1.Items.Add(listbox_text(list_num))
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(e.Index).ToString(), e.Font, myBrush, _
e.Bounds, StringFormat.GenericDefault)
add_item_colour = False
End If
e.DrawFocusRectangle()
End Sub