السلام عليكم ورحمة الله وبركاتة
اضع بين ايديكم مثال لتغيير الوان العناصر داخل ComboBox الكمبو بوكس
كل عنصر بلون معين تختارة انت
لعل ينتفع بة احد
المثال في المرفقات
جزاكم الله خيراً .
-------------------
مع تعديل بسيط على الكود لجعل اللون تبادلي مع الحفاظ على التأثيرات البصرية في حال التمرير فوق العنصر من القائمة المنسدلة
يصبح الكود كالتالي (مثلا) :
كود :
Private Sub CustomComboBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DrawMode = DrawMode.OwnerDrawVariable '' or = DrawMode.OwnerDrawFixed
ComboBox1.Items.Add("1")
ComboBox1.Items.Add("2")
ComboBox1.Items.Add("3")
ComboBox1.Items.Add("4")
ComboBox1.Items.Add("5")
ComboBox1.Items.Add("6")
ComboBox1.Items.Add("7")
ComboBox1.Items.Add("8")
End Sub
Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
' Check if the item is valid
If e.Index = -1 Then
Return
End If
' Get the item's text
Dim itemText As String = ComboBox1.Items(e.Index).ToString()
' Check if the mouse is hovering over the item
Dim isHovering As Boolean = (e.State And DrawItemState.Selected) = DrawItemState.Selected
' Define colors for the item
Dim backColor As Color
Dim foreColor As Color
'=========================================================================
' إختيار لون الخلفية للعنصر أو السطر
Dim t1 As Color = Color.FromArgb(255, 224, 192)
Dim C1 As Brush = New SolidBrush(t1)
Dim C2 As Brush = New SolidBrush(Color.FromArgb(255, 255, 255))
'=========================================================================
If isHovering Then
' Set colors for the hovered state (e.g., blue background with white text)
' لون خط و خلفية العنصر المظلل
backColor = SystemColors.Highlight
foreColor = Color.White
e.DrawBackground()
Else
' Set colors for the normal state (e.g., default background with black text)
backColor = e.BackColor
foreColor = e.ForeColor
' Draw the background of the item ' فرض اللون التبادلي
If ComboBox1.Items(e.Index).ToString() Mod 2 = 0 Then
e.Graphics.FillRectangle(C1, e.Bounds)
Else
e.Graphics.FillRectangle(C2, e.Bounds)
End If
End If
' Create a brush for the text using the determined foreground color
Using textBrush As New SolidBrush(foreColor)
' Draw the item's text
e.Graphics.DrawString(itemText, e.Font, textBrush, e.Bounds)
End Using
' Draw the focus rectangle, if needed
e.DrawFocusRectangle()
End Sub