PDA

View Full Version : سوال: عوض کردن رنگ متن هر آیتم در ListBox



GodFather1
یک شنبه 21 شهریور 1395, 14:47 عصر
سلام
من در حال نوشتن یه برنامه محاسبه نمرات هستم

مشکلم فقط اینجاست که مثلا میخوام کلمه "قبولی" رو با رنگ سبز توی لیست باکس نشون بده و کلمه "رد" رو با رنگ قرمز نشون بده.

از کد زیر استفاده کردم

If c > 70 Then
ListBox2.Items.Add(": Passed")
ListBox2.ForeColor = Color.Green

End If
If c < 70 Then
ListBox2.Items.Add(":NotPassed!")
ListBox2.ForeColor = Color.Red

End If

بعدش که نمره رو وارد می کنم و کلیک می کنم کلمه "passed " به رنگ سبز توی لیست باکس نشون داده میشه

وقتی نمره پایین تر رو وارد می کنم کلمه "Not passed" قرمز میاد توی لیست باکس

مشکل اینجاست که "passed" هم توی لیست باکس رنگش قرمز میشه

اگه سورسی یا روشی هست ممنون میشم

gilsoft
یک شنبه 21 شهریور 1395, 15:43 عصر
سلام دوست عزیز

باید از نمونه کد زیر ایده بگیری:
Private WithEvents ListBox1 As New ListBox()

Private Sub InitializeListBox()
ListBox1.Items.AddRange(New Object() _
{"Red Item", "Orange Item", "Purple Item"})
ListBox1.Location = New System.Drawing.Point(81, 69)
ListBox1.Size = New System.Drawing.Size(120, 95)
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
Controls.Add(ListBox1)

End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles ListBox1.DrawItem

' Draw the background of the ListBox control for each item.
e.DrawBackground()

' Define the default color of the brush as black.
Dim myBrush As Brush = Brushes.Black

' Determine the color of the brush to draw each item based on
' the index of the item to draw.
Select Case e.Index
Case 0
myBrush = Brushes.Red
Case 1
myBrush = Brushes.Orange
Case 2
myBrush = Brushes.Purple
End Select

' Draw the current item text based on the current
' Font and the custom brush settings.
e.Graphics.DrawString(ListBox1.Items(e.Index).ToSt ring(), _
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault)

' If the ListBox has focus, draw a focus rectangle around _
' the selected item.
e.DrawFocusRectangle()
End Sub

موفق باشید .....

ir-security
سه شنبه 30 شهریور 1395, 16:56 عصر
سلام برای اینکه بخوایم با وارد کردن یک ایتم در تکست باکس و زدن باتن اون ایتم مورد نظر رو از لیست باکس (در صورت وجود) پاک کنیم باید از چه دستوری استفاده کنیم؟ ممنونم

336699
سه شنبه 30 شهریور 1395, 17:55 عصر
سلام برای اینکه بخوایم با وارد کردن یک ایتم در تکست باکس و زدن باتن اون ایتم مورد نظر رو از لیست باکس (در صورت وجود) پاک کنیم باید از چه دستوری استفاده کنیم؟ ممنونم

سلام

بهتر بود شما یه موضوع جدید ایجاد میکردید

این طوری کسی سوال رو نمی بینه