تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[VB.NET] listbox تلوين عدد من هذه الأسماء
#1
السلام عليكم ورحمة الله وبركاته

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


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

                                                          شكراً مقدماً                
الرد }}}
تم الشكر بواسطة:
#2
كود :
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
الرد }}}
تم الشكر بواسطة: Ashraf10 , Ashraf10 , mervandz
#3
 الف شكر لك aljzazy
الرد }}}
تم الشكر بواسطة:
#4
لتلوين عناصر محددة في 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");
}
يمكنك تخصيص الشرط والألوان وفقًا لاحتياجاتك الخاصة.
الرد }}}
تم الشكر بواسطة:



التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم