472,780 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 1410
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.