View Full Version : سوال: لیست باکس با سطرهای رنگی
hossein-khoshseyar
پنج شنبه 19 مرداد 1391, 14:14 عصر
دوستان چه طور می تونیم یه لیست باکس داشته باشیم که بشه ملا رنگ یک سطر اون متفاوت از بقیه سطرها باشه
اصلا با لیست باکس خود ویزوال استودیو میشه چنین کاری کرد؟
ممنون میشم اگه جوابم رو بدید
amirzandi
پنج شنبه 19 مرداد 1391, 17:57 عصر
سلام
میشه باید خاصیت DrawMode رو تغییر بدین و با استفاده از Graphics این کار رو بکنید..
اگر عجله ندارید من در حال تهیه این لیست باکس هستم ، به مجموعه کامپوننت اضافه می کنم/
the king
پنج شنبه 19 مرداد 1391, 18:11 عصر
دوستان چه طور می تونیم یه لیست باکس داشته باشیم که بشه ملا رنگ یک سطر اون متفاوت از بقیه سطرها باشه
اصلا با لیست باکس خود ویزوال استودیو میشه چنین کاری کرد؟
ممنون میشم اگه جوابم رو بدید
یک عدد ListBox با نام listBox1 روی فرم Form1 قرار دهید و این کد ها رو برای کلاس Form1 قرار دهید :
Public Class Form1
Private colors As Color() = {Color.LightBlue, Color.LightGreen, Color.LightPink, Color.LightSalmon}
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 colors.Length)
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
hossein-khoshseyar
شنبه 21 مرداد 1391, 13:10 عصر
یک عدد ListBox با نام listBox1 روی فرم Form1 قرار دهید و این کد ها رو برای کلاس Form1 قرار دهید :
Public Class Form1
Private colors As Color() = {Color.LightBlue, Color.LightGreen, Color.LightPink, Color.LightSalmon}
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 colors.Length)
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
ممنون از راهنماییتون
اما من می خوام فقط نوشت هها رنگی بشه نه پس زمینه
ممکنه راهنماییم کنید
خودم اومدم کد شما رو دستکاری کردم اولای اون ایونت رو پاک کردم و و فقط توی اون if اخر رو توش یه رنگ نوشتم
کار می کرد اما همه نوشته هام یه سایه هم داشت و طبیعی نبود
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
If (e.Index >= 0) AndAlso (e.Index < ListBox1.Items.Count) Then ' New SolidBrush(e.ForeColor)
e.Graphics.DrawString(ListBox1.Items(e.Index).ToSt ring(), e.Font, Brushes.Red, e.Bounds, format)
End If
End Sub
ممنون می شم اگه راهنماییم کنید
hossein-khoshseyar
شنبه 21 مرداد 1391, 13:24 عصر
البته الان هم این کد رو نوشتم ولی بازم مشکلم حل نشد
و رنگ همه سطر ها با هم عوض شد
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 = ListBox1.BackColor ' colors(e.Index Mod colors.Length)
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 ' New SolidBrush(e.ForeColor)
e.Graphics.DrawString(ListBox1.Items(e.Index).ToSt ring(), e.Font, Brushes.Red, e.Bounds, format)
End If
End Sub
لطفا را هنماییم کنید
the king
شنبه 21 مرداد 1391, 19:20 عصر
Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim format As New StringFormat()
Dim c As Color
format.LineAlignment = StringAlignment.Center
e.DrawBackground()
e.DrawFocusRectangle()
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
c = e.ForeColor
Else
c = Color.Red
End If
If (e.Index >= 0) AndAlso (e.Index < ListBox1.Items.Count) Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToSt ring(), e.Font, New SolidBrush(c), e.Bounds, format)
End If
End Sub
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.