I want to get a ListView to display some items with a different color. I have been setting the forecolor of the item in the lv_SelectedItemChanged event, by using

item.ForeColor = Color.Red;

This all works fine, except that when the item is selected, it defaults to using the system norm of white text on a blue background. Deselect the item and it displays as red again.

My question is, do I really have to make the ListView owner drawn, just to get the text to display in my chosen color when the item is selected? I'm not sure that getting the color right warrants the necessary work if that is the case!

I'm very interested to hear your answers.

Sorry, I am not sure what you want?

Are you saying you don't want it to go Blue when the selection is made, or do you want it to stay blue when the selection is made?

OK, basically what I want is to be able to set the forecolor (i.e. the text color) of a selected item and I haven't found a simple mechanism to do this. I am quite happy for the backcolor to be the usual blue.

The longer version of what I want to do is set certain text (actually text that needs to be reviewed) to show as red if it is not selected in the listview, and some other equally noticeable color if it is selected. If I debug through my code, the forecolor is set as I expect, but this is not displaying in the listview when the item is selected. That is, it seems that the built in process of selecting the item overrides my forecolor that I set. I end up with white text on a blue background, rather than my expected color of text on the blue background.

Hopefully this clarifies my question.

can you show me what you did?

for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
   listView1.SelectedItems[i].ForeColor = Color.Red;
}

OK. I have just been playing round with this, so I haven't ever had it working with all the logic in place. But to answer your question, say I have added a number of items to my listview, each with one subitem. To try out changing the text color, in MyLV__SelectedIndexChanged, I added the following code:

foreach (ListViewItem item in MyListView.Items)
    {
        item.ForeColor = Color.Red;
    }

For those items that are not selected, the text is indeed red. For the selected item, the text is white (on the blue background). If I then change the selection, the previously selected item now has red text. So, all unselected items have red text, the selected item does not.

How can I set the forecolor / text color of selected items?

for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
   listView1.SelectedItems[i].ForeColor = Color.Red;
}

Yes that does set the text to red for the selected items - but only if they are not highlighted as selected, in which case they are shown as white text on blue background, and this is exactly the problem I have come up against. I was already able to successfully color the items when they were not selected, what I was trying to do was make them stand out when they were highlighted as selected too. Do you know if I can do this?

You can also create OnwerDraw list.

private void Form1_Load(object sender, EventArgs e)
        {
            listView1.OwnerDraw = true;
            listView1.DrawItem += new DrawListViewItemEventHandler(listView1_DrawItem);
        }

        void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {

            e.DrawBackground();
            e.DrawFocusRectangle();
            if (e.Item.Selected)
                e.Graphics.DrawString(e.Item.Text, new Font("Arial", 10), Brushes.Red, e.Item.Position);
            else
                e.Graphics.DrawString(e.Item.Text, new Font("Arial", 10), Brushes.Black, e.Item.Position);
                
        }

You can also create OnwerDraw list.

private void Form1_Load(object sender, EventArgs e)
        {
            listView1.OwnerDraw = true;
            listView1.DrawItem += new DrawListViewItemEventHandler(listView1_DrawItem);
        }

        void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {

            e.DrawBackground();
            e.DrawFocusRectangle();
            if (e.Item.Selected)
                e.Graphics.DrawString(e.Item.Text, new Font("Arial", 10), Brushes.Red, e.Item.Position);
            else
                e.Graphics.DrawString(e.Item.Text, new Font("Arial", 10), Brushes.Black, e.Item.Position);
                
        }

Thanks, adatapost. I was hoping to do this without using an owner drawn listview, as I will also have to draw the subitems, images, etc. myself, and I think that involves more work than I have time for in this project. I may just have to put up with white on blue for selected items.

Try using the Enter or Leave Events and set the color of the current Selectedindex to forecolor white

listView1.SelectedItems[0].ForeColor = Color.White;

so on leave you just add that code there. Only the selected Row's Forecolor will change.

Try using the Enter or Leave Events and set the color of the current Selectedindex to forecolor white

listView1.SelectedItems[0].ForeColor = Color.White;

so on leave you just add that code there. Only the selected Row's Forecolor will change.

I am able to set the forecolor - it just doesn't appear that the listview uses that forecolor when displaying a selected item (whether the control has focus or not).

What this means is that despite the fact the forecolor is what I set it to be, the selected item is displayed as white on blue if the listview is the active control, or black on grey if it is inactive.

Does anyone know of a way to change the colors used for selected items (other than owner drawing)?

Thanks finito, that looks very interesting. It certainly looks like it addresses my problem. I will have a play with it as soon as I get a chance.
Sorry about the delayed response. I have been away on leave for the last two weeks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.