Jalapeno
Jalapeno

Reputation: 202

Can't Get ListViewItem to Display in Color

So I'm using C# in a Windows Forms Application and my question here is about the ListView. All I want to do is display the ListViewItem in a certain colour depending on the value. Now I have tried the traditional method of:

ListViewItem li = new ListViewItem();
li.ForeColor = Color.Green;
li.Text = "Limit: " + wc.getCreditLimit();
listLimits.Items.Add(li);

But the result I see in the ListView is:

ListViewItem:{Limit:15000}

And no, it's not coloured

Another method I tried was to initialise the ListViewItem with text as an argument like so:

ListViewItem li = new ListViewItem("Limit: " +   wc.getCreditLimit());
li.ForeColor = Color.Green;
listLimits.Items.Add(li);

But that also produces the same result as before.

Another Method I tried was do add the Text property of the ListViewItem like so:

ListViewItem li = new ListViewItem("Limit: " + wc.getCreditLimit());
li.ForeColor = Color.Green;
listLimits.Items.Add(li.Text);

Now this displays the correct text in the ListView, however It's still not coloured!

Can someone please explain this odd behaviour because from what I've read, it looks like this is the only approach to adding colour to a ListViewItem. Any advice would be appreciated, thanks.

Upvotes: 0

Views: 93

Answers (2)

TaW
TaW

Reputation: 54453

Your code (first paragraph) works just fine.

If you see ListViewItem:{Limit:15000} you have a problem elsewhere.

It looks as if you added a wrong object type, but as it clearly is a ListViewItem you don't..

So I guess you are adding it to a ListBox instead of a ListView.. When I do that I get what you see:

enter image description here

Upvotes: 1

Rafał Głowacki
Rafał Głowacki

Reputation: 1

Try this:

listLimits.Items.Add(li);

You are adding just the text not ListViewItem

listLimits.Items.Add(**li.Text**);

Upvotes: 0

Related Questions