منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : listbox تلوين عدد من هذه الأسماء
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
السلام عليكم ورحمة الله وبركاته

لدي ListBox وفيها مجموعة من الأسماء ، كيف يمكن تلوين عدد من هذه الأسماء بحيث يتوافق مع شرط معين ، بمعنى لو


 كان الأسم samir يكون لون  الأسم أخضر  ولو كان الأسم monir يكون لون  الأسم الاحمر وهكذا...

                                                          شكراً مقدماً                
كود :
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
 الف شكر لك aljzazy
لتلوين عناصر محددة في ListBox وفقًا لشرط معين، يمكنك استخدام حدث DrawItem لتعديل الرسم التوضيحي لكل عنصر في ListBox. كيف يمكن تحقيق ذلك في C#:

قم بتعيين DrawMode لـ OwnerDrawFixed في ListBox.
قم بتعريف حدث DrawItem للقيام برسم العناصر بشكل مخصص.
قد يبدو الكود كما يلي:

csharp
Copy code
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
// افحص العنصر الحالي
if (e.Index >= 0)
{
string currentItem = listBox1.Items[e.Index].ToString();

// قم بتحديد اللون وفقًا للشرط المحدد
Color itemColor = GetItemColor(currentItem);

// رسم النص باللون المحدد
using (Brush brush = new SolidBrush(itemColor))
{
e.Graphics.DrawString(currentItem, e.Font, brush, e.Bounds, StringFormat.GenericDefault);
}
}
}

// تحديد لون كل عنصر بناءً على شرط معين
private Color GetItemColor(string itemName)
{
// ضع هنا الشروط والألوان المطلوبة
if (itemName.Equals("samir", StringComparison.OrdinalIgnoreCase))
{
return Color.Green;
}
else if (itemName.Equals("monir", StringComparison.OrdinalIgnoreCase))
{
return Color.Red;
}
// إذا لم يتوافق الشرط مع أي قاعدة، فقم بتحديد لون افتراضي
return SystemColors.WindowText;
}
تأكد من ربط حدث DrawItem مع الدالة listBox1_DrawItem:

csharp
Copy code
private void Form1_Load(object sender, EventArgs e)
{
// ربط حدث DrawItem مع الدالة listBox1_DrawItem
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);

// إضافة بعض الأسماء لـ ListBox للتجربة
listBox1.Items.Add("samir");
listBox1.Items.Add("monir");
listBox1.Items.Add("john");
listBox1.Items.Add("doe");
}
يمكنك تخصيص الشرط والألوان وفقًا لاحتياجاتك الخاصة.