12-11-23, 10:40 PM
كود :
Imports System.Windows.Forms
Imports System.Drawing
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Populate the ListBox with items
ListBox1.Items.Add("Item 1")
ListBox1.Items.Add("Item 2")
ListBox1.Items.Add("Item 3")
' Enable owner drawing for the ListBox
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub
Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
' Check if the item index is valid
If e.Index >= 0 Then
' Get the ListBox
Dim listBox As ListBox = DirectCast(sender, ListBox)
' Get the item text
Dim itemText As String = listBox.Items(e.Index).ToString()
' Choose a color based on the item text (you can use any logic here)
Dim itemColor As Color = If(itemText.Contains("2"), Color.Red, Color.Black)
' Create a brush with the chosen color
Dim textBrush As New SolidBrush(itemColor)
' Draw the item text with the chosen color
e.Graphics.DrawString(itemText, e.Font, textBrush, e.Bounds.X, e.Bounds.Y)
' Dispose of the brush to free resources
textBrush.Dispose()
' If the item is selected, draw a focus rectangle around it
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.DrawFocusRectangle()
End If
End If
End Sub
End Class
