PDA

View Full Version : سوال: اضافه کردن رنگ به لیست باکس



masoud.a
یک شنبه 16 مهر 1391, 12:09 عصر
سلام
چطور میتونم سطرهای لیست باکس رو رنگی کنم؟
این کارو تو سی شارپ نوشتم.با این کد:

Color back = ((e.Index % 2) == 0) ? Color.Yellow : Color.Lavender;
Color fore = e.ForeColor;

93690

اینم شکلشه

hakim22
یک شنبه 16 مهر 1391, 21:20 عصر
اگر هدف وسیله رو توجیح میکنه !!!!؟
از TreeView استفاده کنید. با کمی تنظیم دقیقا مثل لیست باکس میشه و در حالی که میشه هر نود رو تغییر رنگ داد.

xxnagin
یک شنبه 16 مهر 1391, 21:51 عصر
سلام
لیست باکس با سطرهای رنگی (http://barnamenevis.org/showthread.php?355618-لیست-باکس-با-سطرهای-رنگی&highlight=%D9%84%DB%8C%D8%B3%D8%AA+%D8%A8%D8%A7%DA %A9%D8%B3+%D8%A8%D8%A7+%D8%B3%D8%B7%D8%B1%D9%87%D8 %A7%DB%8C+%D8%B1%D9%86%DA%AF%DB%8C)

Public Class Form1
Private colors As Color() = {Color.Yellow, Color.Red}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("Item 1")
ListBox1.Items.Add("Item 2")
ListBox1.Items.Add("Item 3")
ListBox1.Items.Add("Item 4")
ListBox1.ItemHeight = 18
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim format As New StringFormat()
format.LineAlignment = StringAlignment.Center
Dim c As Color = colors(e.Index Mod 2)
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
c = Color.FromArgb(Math.Max(CInt(c.R) - 50, 0), Math.Max(CInt(c.G) - 50, 0), Math.Max(CInt(c.B) - 50, 0))
End If
e.Graphics.FillRectangle(New SolidBrush(c), e.Bounds)
e.DrawFocusRectangle()
If (e.Index >= 0) AndAlso (e.Index < ListBox1.Items.Count) Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToSt ring(), e.Font, New SolidBrush(e.ForeColor), e.Bounds, format)
End If
End Sub
End Class

masoud.a
چهارشنبه 19 مهر 1391, 23:20 عصر
سلام
لیست باکس با سطرهای رنگی (http://barnamenevis.org/showthread.php?355618-لیست-باکس-با-سطرهای-رنگی&highlight=%D9%84%DB%8C%D8%B3%D8%AA+%D8%A8%D8%A7%DA %A9%D8%B3+%D8%A8%D8%A7+%D8%B3%D8%B7%D8%B1%D9%87%D8 %A7%DB%8C+%D8%B1%D9%86%DA%AF%DB%8C)

Public Class Form1
Private colors As Color() = {Color.Yellow, Color.Red}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("Item 1")
ListBox1.Items.Add("Item 2")
ListBox1.Items.Add("Item 3")
ListBox1.Items.Add("Item 4")
ListBox1.ItemHeight = 18
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim format As New StringFormat()
format.LineAlignment = StringAlignment.Center
Dim c As Color = colors(e.Index Mod 2)
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
c = Color.FromArgb(Math.Max(CInt(c.R) - 50, 0), Math.Max(CInt(c.G) - 50, 0), Math.Max(CInt(c.B) - 50, 0))
End If
e.Graphics.FillRectangle(New SolidBrush(c), e.Bounds)
e.DrawFocusRectangle()
If (e.Index >= 0) AndAlso (e.Index < ListBox1.Items.Count) Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToSt ring(), e.Font, New SolidBrush(e.ForeColor), e.Bounds, format)
End If
End Sub
End Class


ممنون از جوابتون سطرهای لیست باکس رنگی شد ولی وقتی یکیشون رو انتخاب میکنم چرا error میگیره؟
93825

xxnagin
پنج شنبه 20 مهر 1391, 13:00 عصر
به جای عدد 2 colors.Length را بزار ببین درست میشه
اون لینک بالا هم یه نگاه بکن
توی یه پروژه جدید فقط این کد رو امتحان کن چون این کد برا من که کار میکنه احتمالا کدهای خودت مشکل داره نه این کد

masoud.a
پنج شنبه 20 مهر 1391, 13:51 عصر
لیست باکس رنگی رو ایجاد میکنه . تو برنامم قبلا وقتی Enter میزدم یسری کارهائی انجام میداد ولی الان این اشکال ایجاد میشه.
یعنی اینو بنویسم؟
e.Index Mod colors.Length
ولی تاثیری نداشت.

xxnagin
پنج شنبه 20 مهر 1391, 17:03 عصر
شما تو یه پروژه جدا این یه تیکه کد بالا رو امتحان کن
کد بالا هیچ مشکلی نداره چه با 2 چه با colors.Length
احتمالا در قسمت ListBox1_SelectedIndexChanged تو پروژه رو چک کن ببین درسته کدش و کدش رو بذار

lpsklpsk
دوشنبه 15 مهر 1392, 11:19 صبح
سلام
چطور میتونم سطرهای لیست باکس رو رنگی کنم؟
این کارو تو سی شارپ نوشتم.با این کد:

Color back = ((e.Index % 2) == 0) ? Color.Yellow : Color.Lavender;
Color fore = e.ForeColor;

93690

اینم شکلشه

سلام دوست من
این کد رو تو سی شارپ جواب گرفتی شما؟؟؟
من چرا جواب نگرفتم پس؟
این تکه کد رو تویه draw نوشتم
ارور نداره اما جواب نمیده
خصوصیات لیست باکسمو باید تغییر خاصی بدم؟

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
MessageBox.Show("ok");
Color back = ((e.Index % 2) == 0) ? Color.Yellow : Color.Red;
Color fore = e.ForeColor;
}

lpsklpsk
دوشنبه 15 مهر 1392, 11:20 صبح
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
MessageBox.Show("ok");
Color back = ((e.Index % 2) == 0) ? Color.Yellow : Color.Red;
Color fore = e.ForeColor;
}