473,765 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ValueM ember = "ID"
ListBox1.DataSo urce = dv1
ListBox1.Displa yMember = "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 1442
Try something like the following.

Private DataTable1 As DataTable

....

DataTable1 = New DataTable("MyTa ble")
DataTable1.Colu mns.Add("ID", Type.GetType("S ystem.String"))
DataTable1.Colu mns.Add("Name", Type.GetType("S ystem.String"))
DataTable1.Colu mns.Add("Catego ry", Type.GetType("S ystem.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.ToStri ng(), rand.Next(0, 4).ToString()})
Next

Me.ListBox1.Dis playMember = "Name"
Me.ListBox1.Val ueMember = "ID"
Me.ListBox1.Dat aSource = DataTable1.Defa ultView

....

Private Sub ListBox1_DrawIt em(ByVal sender As Object, ByVal e As
System.Windows. Forms.DrawItemE ventArgs) Handles ListBox1.DrawIt em
Dim list As ListBox = DirectCast(send er, ListBox)
Dim row As DataRow = DirectCast(list .Items(e.Index) , DataRowView).Ro w
Dim textBrush As Brush

e.DrawBackgroun d()

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.Draw String(DirectCa st(row("Name"), String), list.Font,
textBrush, New RectangleF(e.Bo unds.X, e.Bounds.Y, e.Bounds.Width,
e.Bounds.Height ))

e.DrawFocusRect angle()
End Sub

--
Tim Wilson
..Net Compact Framework MVP

"Richard Wilde" <ri*****@yahoo. com> wrote in message
news:ue******** *****@TK2MSFTNG P15.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.ValueM ember = "ID"
ListBox1.DataSo urce = dv1
ListBox1.Displa yMember = "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
4294
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
45082
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 moves up or down the list. At the moment its the default deep purple colour you get in any windows app, but I'd like change this to a more suitable colour for my site. This is what I have already - <html>
6
24447
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 declaration looks like this: map<int, list<my_class*> > map_test; Now, how do I go about adding and accessing a new element to any given list? How would I access the data? I did this with a list of pointers, but can't figure out how to get the map...
1
1383
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 should have different background, additionaly I'd like the background to change (3rd color) when mouse is hovering over the row. Moreover I'd like to support drag 'n' drop on each item. User cannot change anything on the list.
2
1370
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 styles arguments for string. Is it possible ? If yes, please give me an example . regards, Mihai
6
3119
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 fired - hence running the code pertaining to that first menu item. How can I do this in code? Thanks, Brad
1
1465
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 I want to do is to be able to List the ROWS of data either a RED or GREEN colour depending upon an IF statement. So... IF X =< myVariable then Row would be GREEN ELSE Row would be RED.
6
1060
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 something like m3(). As currently written it does not work, and I have tried different ways, but I haven't managed to make it work. Is there a possibility ? Or is m2() the optimum ?
4
6818
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 background colour of the currently selected item to red. Lets say in this case it was the 3rd row. The 3rd row is still the current item, so its still got a blue background. Now in the past if I scrolled down or clicked on another row, I would...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.