Changing colours of certain items in a listbox

  • Thread starter Thread starter Brett Miller
  • Start date Start date
B

Brett Miller

Hi,

I need to change the colour of certain items in a listbox depending on
criteria I receive from my dataset? How would this be accomplished?

Thanks in advance...

BM
 
You can't change the individual back and for colors of a listbox item. You
can change the back or the for color for the control as a whole.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
I need to change the colour of certain items in a listbox depending on
criteria I receive from my dataset? How would this be accomplished?

Add a listbox to a form and try this:

<Code>

Public Sub New()
MyBase.New()
InitializeComponent()

ListBox1.DrawMode = DrawMode.OwnerDrawFixed

ListBox1.Items.Add("Black")
ListBox1.Items.Add("Red")
ListBox1.Items.Add("Green")
ListBox1.Items.Add("Blue")
ListBox1.Items.Add("Pink")

End Sub

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

Dim s As String = ListBox1.Items(e.Index).ToString
Dim b As New SolidBrush(Color.FromName(s))

e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(s, Font, b, e.Bounds.X, e.Bounds.Y)

b.Dispose

End Sub

</Code>

Hope this helps

Blu
 
I didnt think of that, well done !

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
* "Brett Miller said:
I need to change the colour of certain items in a listbox depending on
criteria I receive from my dataset? How would this be accomplished?

I suggest using a listview control instead because it natively supports
setting the color of the items. For the listbox control, you would have
to draw the items in the specific color yourself.
 
hi guys,

Thanks for the response, I can seem to the .Drawmode property or the
..DrawItem event??? I'm coding for the compact framework, mybe thats the
issue?

regards,
bm
 
* "Brett Miller said:
Thanks for the response, I can seem to the .Drawmode property or the
.DrawItem event??? I'm coding for the compact framework, mybe thats the
issue?

For Compact Framework-related questions, consider posting to this group:

<URL:
 
Back
Top