ListView Item Color

Cassio

Junior Contributor
Joined
Nov 30, 2002
Messages
276
Location
Rio de Janeiro
Hi! How can I set the Color of a single subitem?

I tried
Visual Basic:
listItem.SubItems(1).ForeColor = Color.Red

but it has no effect.

Thanks.
 
Unfortunately, ListView rows must all be the same color; SubItems
will take on the color of their parent.
 
Well actually you can have multiple items in a listview each one with different colors, first you need to declare a variable that will contain the Color class

Code:
Dim Clr as System.Drawing.Color

After that start filling your Listview with your items and subitems, but after setting the first column you need to set the color

Code:
ListView1.Items.Add("Item One")
ListView1.Items.Item(i).ForeColor = Clr.Red
ListView1.Items(i).SubItems.Add("SubItem One")

All subitems will take the color, the example should look something like this

Code:
Private Sub Button1_Click (ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim i as Integer
Dim Clr as System.Drawing.Color

For i=0 to 100
     ListView1.Items.Add("Item " & i)
     ListView1.Items.Item(i).ForeColor = Clr.Red
     ListView1.Items(i).SubItems.Add("SubItem " & i)
Next
End Sub

You can change the color for each item in the listview, try it out, and let me know if you have any problems

Regards
 
SOLUTION:
I change the color of individual cells in a List View control.
I highlight cells in pink to show errors.

Visual Basic:
lvwStats.Items(iIndex).SubItems(3).BackColor = Color.LightPink

Normally my ListView is all white, with scattered cells highlighted in pink.
 

Attachments

You have to set up for column independence at item-add time.
Example:
Visual Basic:
itemNew.SubItems.Add("item1") 
itemNew.SubItems.Add("item2")       
itemNew.UseItemStyleForSubItems = False ' Cell independence...
lvwTest.Items.AddRange(New ListViewItem() {itemNew})
Hey, I finally stumped the moderators on one!
Do I get a badge or something?

.
 
Back
Top