473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

list item colour

I am trying to change a colour of a listbox item depending on a value in a
data view
My fields in the dataview are
ID
Name
idCat

ListBox1.ValueMember = "ID"
ListBox1.DataSource = dv1
ListBox1.DisplayMember = "Name"
I have set the listbox drawMode to OwnerdrawFixed and want to be able to
display the name in a different color depending on the idCat. This is where
i have run into problems. I cannot retrieve the idCat from any of the passed
in parameters... Can any one help?

DrawItem event...
dim idCat as int32

idCat = <<HERE>>

Select Case (e.Index)
Case 0
myBrush = Brushes.Red
Case 1
myBrush = Brushes.Orange
Case 2
myBrush = Brushes.Purple
End Select

draw...
Nov 21 '05 #1
2 1424
Try something like the following.

Private DataTable1 As DataTable

....

DataTable1 = New DataTable("MyTable")
DataTable1.Columns.Add("ID", Type.GetType("System.String"))
DataTable1.Columns.Add("Name", Type.GetType("System.String"))
DataTable1.Columns.Add("Category", Type.GetType("System.Int32"))

Dim rand As New Random
Dim iterator As Integer
For iterator = 0 To 25
DataTable1.Rows.Add(New Object() {Guid.NewGuid().ToString(), "Name" +
iterator.ToString(), rand.Next(0, 4).ToString()})
Next

Me.ListBox1.DisplayMember = "Name"
Me.ListBox1.ValueMember = "ID"
Me.ListBox1.DataSource = DataTable1.DefaultView

....

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim list As ListBox = DirectCast(sender, ListBox)
Dim row As DataRow = DirectCast(list.Items(e.Index), DataRowView).Row
Dim textBrush As Brush

e.DrawBackground()

Select Case (DirectCast(row("Category"), Integer))
Case 0
textBrush = Brushes.Red
Case 1
textBrush = Brushes.Green
Case 2
textBrush = Brushes.Blue
Case 3
textBrush = Brushes.Orange
End Select

e.Graphics.DrawString(DirectCast(row("Name"), String), list.Font,
textBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width,
e.Bounds.Height))

e.DrawFocusRectangle()
End Sub

--
Tim Wilson
..Net Compact Framework MVP

"Richard Wilde" <ri*****@yahoo.com> wrote in message
news:ue*************@TK2MSFTNGP15.phx.gbl...
I am trying to change a colour of a listbox item depending on a value in a
data view
My fields in the dataview are
ID
Name
idCat

ListBox1.ValueMember = "ID"
ListBox1.DataSource = dv1
ListBox1.DisplayMember = "Name"
I have set the listbox drawMode to OwnerdrawFixed and want to be able to
display the name in a different color depending on the idCat. This is where i have run into problems. I cannot retrieve the idCat from any of the pass ed in parameters... Can any one help?

DrawItem event...
dim idCat as int32

idCat = <<HERE>>

Select Case (e.Index)
Case 0
myBrush = Brushes.Red
Case 1
myBrush = Brushes.Orange
Case 2
myBrush = Brushes.Purple
End Select

draw...

Nov 21 '05 #2
Tim

Many thanks. This has helped a lot.
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike | last post by:
How do I extract a list of lists from a user defined function and print the results as strings for each list?
2
by: Colin Steadman | last post by:
I have a dropdown list with a couple of dozen entries. I can change the backgroup and text colour. But I cant find the CSS attribute which changes the highlight colour which appears as the mouse...
6
by: gthorne | last post by:
Ok, so I'm trying to code a map of lists of pointers to a class (that contains a struct). Basically, the class holds the struct that I'm populating, and there can be multiple of these. The...
1
by: Piotrek Stachowicz | last post by:
Hi there, I'd like to display list of items in my form. Each item has 3 properties i.e. description , picture (1 of 3 kinds), and url. I want to display it in nice way, e.g. every second row...
2
by: Mihai | last post by:
In a list box I have on each line a combination of string1 + String2. I want to write string 1 with a colour and string2 with another colour. I tried to use string1.format but I cannot find any...
6
by: Brad Pears | last post by:
As part of my vb.net 2005 application I would like to automatically select the very first item (index 0) in a listview containing menu items. That way the the listviews "afterselect" event would...
1
by: Mr. B | last post by:
I've not programmed for a while in VB 2003... but I've decided to modlfiy a Project I have. Seems simple enough... I've a List Box... I fill it with about 30 lines of data (3-4 columns). What...
6
by: Yves Dorfsman | last post by:
In the following script, m1() and m2() work fine. I am assuming m2() is faster although I haven't checked that (loops through the list twice instead of once). Now what I am trying to do is...
4
by: ari | last post by:
I have recently upgraded from vb.net 2002 to 2005, and have encountered the following problem. I have a listview with say a dozen items. Following a user action I programmatically update the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.