12-09-25, 05:42 AM (آخر تعديل لهذه المشاركة : 12-09-25, 05:43 AM {2} بواسطة abo ragab.)
السلام عليكم ورحمة الله وبركاتة اضع بين ايديكم مثال لتغيير الوان العناصر داخل ComboBox الكمبو بوكس كل عنصر بلون معين تختارة انت لعل ينتفع بة احد المثال في المرفقات
عن أبي هريرة - رضي الله عنه - قال: قال رسول الله - صلى الله عليه وسلم -: ((كلمتان خفيفتان على اللسان، ثقيلتان في الميزان، حبيبتان إلى الرحمن: سبحان الله وبحمده، سبحان الله العظيم))؛ متفق عليه.
-------------------
مع تعديل بسيط على الكود لجعل اللون تبادلي مع الحفاظ على التأثيرات البصرية في حال التمرير فوق العنصر من القائمة المنسدلة
يصبح الكود كالتالي (مثلا) :
كود :
Private Sub CustomComboBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DrawMode = DrawMode.OwnerDrawVariable '' or = DrawMode.OwnerDrawFixed
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
15-09-25, 01:10 AM (آخر تعديل لهذه المشاركة : 15-09-25, 01:11 AM {2} بواسطة abo ragab.)
(13-09-25, 11:58 AM)salamandal كتب : جزاك الله خيرا
اللهم أمين وإياك
(14-09-25, 03:02 AM)Taha Okla كتب : جزاكم الله خيراً .
-------------------
مع تعديل بسيط على الكود لجعل اللون تبادلي مع الحفاظ على التأثيرات البصرية في حال التمرير فوق العنصر من القائمة المنسدلة
يصبح الكود كالتالي (مثلا) :
كود :
Private Sub CustomComboBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.DrawMode = DrawMode.OwnerDrawVariable '' or = DrawMode.OwnerDrawFixed
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
اللهم أمين وإياك
عن أبي هريرة - رضي الله عنه - قال: قال رسول الله - صلى الله عليه وسلم -: ((كلمتان خفيفتان على اللسان، ثقيلتان في الميزان، حبيبتان إلى الرحمن: سبحان الله وبحمده، سبحان الله العظيم))؛ متفق عليه.