I\'m making a small program in VB.NET. I have a listBox and a button. I\'d like to be able to press the button and have the selected listBox item change it\'s foreColor to g
You need to handle DrawItem event and DrawMode=OwnerDrawFixed property.
Dim buttonPressed As Boolean
Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
If ListBox1.SelectedIndices.Contains(e.Index) And buttonPressed Then
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Green, e.Bounds.X, e.Bounds.Y)
Else
e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)
End If
If e.Index = ListBox1.Items.Count - 1 Then
buttonPressed = False
End If
e.DrawFocusRectangle()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
buttonPressed = True
ListBox1.Refresh()
End Sub