Windows Develop Bookmark and Share   
 index > Windows Forms General > Help with setting color in ListView.SubItem
 

Help with setting color in ListView.SubItem

Need some help. I was wondering if I can set the color of only a subitem in my ListView.

For example. I have a ListView that I will be adding two items: invoiceID and balance.

I want the invoiceID (first item) to be black, but the balance (second item) to be in red.

So I have:

ListViewItem lvi = new ListViewItem(invoiceID.ToString());
lvi.SubItems.Add(balance.ToString(), Color.Red, Color.White, new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold));

ListView1.Items.Add(lvi);

I am successful in displaying the values of invoiceID and balance in my ListView1. But it just isn't red.

Please help.

NOTE* When I simply try to set the lvi.ForeColor = Color.Red. The entire row is in red, but I just want the second item (in the second column to be in red) while the rest to be in black.


MTech8  Saturday, September 12, 2009 11:52 PM
Ofttimes the help files are a better source of information than the forums:

ListViewItem.ListViewSubItem.ForeColor Property

You can use the ForeColor property to change the color of the subitem text. This property can be used if you want to use different background and foreground color combinations (using the BackColor property to set the background color) to differentiate one item from another. For example, you could set the ForeColor property to Red to identify items that have a negative number associated with them.

If the UseItemStyleForSubItems property of the ListViewItem that owns the subitem is set to true, setting this property has no effect.

  • Marked As Answer byMTech8 Sunday, September 13, 2009 7:42 AM
  •  
JohnWein  Sunday, September 13, 2009 7:30 AM
lvi is of type ListViewItem. I think you need to set this on the object that is of type ListViewItem.ListViewSubItem. The Add method you are callingwill returns the instance you're looking for!
BinaryCoder  Sunday, September 13, 2009 12:15 AM
lvi is of type ListViewItem. I think you need to set this on the object that is of type ListViewItem.ListViewSubItem. The Add method you are callingwill returns the instance you're looking for!

Thanks BinaryCoder. I believe I was setting ListViewItem.ListViewSubItem.Add method in my second line of my code:

lvi.SubItems.Add(balance.ToString(), Color.Red, Color.White, new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold));

I used the 3rd constructor provided. The first 3 parameters are pretty straight forward. I'm guessing it's the 4th paramenter with stipulating the Font that might be making my code not work.

Can anyone help with this?

Can anyone confirm that it is possible to have different subitems be different colors when they are in the same row?
MTech8  Sunday, September 13, 2009 4:30 AM
Ofttimes the help files are a better source of information than the forums:

ListViewItem.ListViewSubItem.ForeColor Property

You can use the ForeColor property to change the color of the subitem text. This property can be used if you want to use different background and foreground color combinations (using the BackColor property to set the background color) to differentiate one item from another. For example, you could set the ForeColor property to Red to identify items that have a negative number associated with them.

If the UseItemStyleForSubItems property of the ListViewItem that owns the subitem is set to true, setting this property has no effect.

  • Marked As Answer byMTech8 Sunday, September 13, 2009 7:42 AM
  •  
JohnWein  Sunday, September 13, 2009 7:30 AM

JohnWein. Thank you so much for the help.

Prior to and after posting to the forums. I have been searching the help files and MSDN libraries to try to find an answer to my question. At times it is very daunting trying to find the answer amist so much information.

What I was missing was the lvi.UseItmeStyleForSubItems = false. This allowed the code to work properly.

MTech8  Sunday, September 13, 2009 7:45 AM

You can use google to search for other answers

Custom Search

More Threads

• Drawing a control in a Transparent Panel
• Passing variables between forms in VS2005
• treeviews
• XML Stream, Styled, into WebBrowser control
• How can i copy the highlighted text from the CURRENT active window to the clipboard using c#?
• Problem Printing a datagrid in Visual C#
• DataGridViewComboBoxCell Don't Drop Down
• Richtextbox help...
• How to serialize controls and deserialized them on next application start?
• Bitmap.Save