Listbox selected color
Made by request @Predator
Learn to read the code, know what it does, and how it does it. Don't just C+P please.
That is essential. It gets the selected item.
That's telling them to change the color if they selected.
This is the color. You can edit it as you want.
This declares the whole thing making you able to use it. You cannot do anything without this. It's also very essential.
Thanks + Rep if you've liked the tutorial.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
For i = 1 To 10
Me.ListBox1.Items.Add(New ListBoxItem("Item " & i))
Next
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim item As ListBoxItem = DirectCast(Me.ListBox1.SelectedItem, ListBoxItem)
If item IsNot Nothing Then
item.PaintSpecial = True
Me.ListBox1.Refresh()
End If
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, _
ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem
Dim item As ListBoxItem = DirectCast(Me.ListBox1.Items(e.Index), ListBoxItem)
If item.PaintSpecial Then
e.Graphics.FillRectangle(Brushes.Red, e.Bounds)
Else
e.DrawBackground()
End If
e.Graphics.DrawString(item.Value, e.Font, Brushes.Black, e.Bounds)
End Sub
End Class
Public Class ListBoxItem
Private _paintSpecial As Boolean
Private _value As String
Public Property PaintSpecial() As Boolean
Get
Return Me._paintSpecial
End Get
Set(ByVal value As Boolean)
Me._paintSpecial = value
End Set
End Property
Public Property Value() As String
Get
Return Me._value
End Get
Set(ByVal value As String)
Me._value = value
End Set
End Property
Public Sub New(ByVal value As String)
Me._value = value
End Sub
End Class
Code:
For i = 1 To 10
Me.ListBox1.Items.Add(New ListBoxItem("Item " & i))
Next
Code:
Dim item As ListBoxItem = DirectCast(Me.ListBox1.SelectedItem, ListBoxItem)
If item IsNot Nothing Then
item.PaintSpecial = True
Me.ListBox1.Refresh()
End If
Code:
If item.PaintSpecial Then
e.Graphics.FillRectangle(Brushes.Red, e.Bounds)
Else
e.DrawBackground()
End If
e.Graphics.DrawString(item.Value, e.Font, Brushes.Black, e.Bounds)
Code:
Public Class ListBoxItem
Private _paintSpecial As Boolean
Private _value As String
Public Property PaintSpecial() As Boolean
Get
Return Me._paintSpecial
End Get
Set(ByVal value As Boolean)
Me._paintSpecial = value
End Set
End Property
Public Property Value() As String
Get
Return Me._value
End Get
Set(ByVal value As String)
Me._value = value
End Set
End Property
Public Sub New(ByVal value As String)
Me._value = value
End Sub
End Class
Thanks + Rep if you've liked the tutorial.