-1

the ListView I am using contains some subitems which the user can click on (and some he cannot). These are the same for each row. I mark these clickable subitems with a certain backcolor.

But when the whole row is selected (and FullRowSelect = true), the highlighting sets the same backcolor to all subitmes.

What I would like to do is to show to the user which row he selected, but still keep the different backcolors for the subitems. I played a little bit which owner drawing the ListView, but could not find a way to do this.

Thanks for your help! Tom

TaW
  • 53,122
  • 8
  • 69
  • 111
Tom
  • 5
  • 4
  • Have you seen this one: http://stackoverflow.com/questions/1228492/winforms-listview-selection-drawing – DonBoitnott Nov 01 '14 at 11:55
  • Also a lot of good points here: http://stackoverflow.com/questions/5179664/how-to-change-listview-selected-row-backcolor-even-when-focus-on-another-control – DonBoitnott Nov 01 '14 at 11:58
  • Make sure not to go for Stan's answer in the 1st link! Or for the otherone, since it invlove a 3rd party control. Instead, simply believe it: Owner-Drawing is not so hard at all..!! The ListView is in View = Details, I assume? – TaW Nov 01 '14 at 12:01
  • @TaW: Yes, it is set to Details. – Tom Nov 01 '14 at 12:07
  • I am using the code example from http://msdn.microsoft.com/de-de/library/system.windows.forms.listview.ownerdraw%28v=vs.110%29.aspx for over an hour but could not succeed. But I will have a look at the links you posted. – Tom Nov 01 '14 at 12:08
  • @DonBoitnott: I checked the two links you mentioned. The second I already knew, but read again. Both unfortunately did not solve my problem. Any more suggestions anybody? – Tom Nov 01 '14 at 12:24
  • @Tom: See my answer and note how simple it really is. I have no idea where those complicated suggestions come from; maybe from a long gone past..? – TaW Nov 01 '14 at 12:57
  • @TaW: I tried your code example and it worked for me too! :-) I was always looking a the wrong stuff and trying too many things at the same time... ;-) I wouldn't have found the solution without your code example, thank's a lot!!! – Tom Nov 01 '14 at 13:47

1 Answers1

1

You need to set the ListView to OwnerDraw and use code like this:

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    if ( e.SubItem.Name != "click1") e.DrawDefault = true; 
    else
    {
        if (e.Item.Selected)
              e.Graphics.FillRectangle(Brushes.SeaShell, 
                                       new Rectangle(e.Bounds.Location, e.Bounds.Size));
        else  e.Graphics.FillRectangle(Brushes.WhiteSmoke, 
                                       new Rectangle(e.Bounds.Location, e.Bounds.Size));
        e.DrawText();
    }
}

private void listView1_DrawColumnHeader(object sender, 
                                        DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

Obviously you will adapt not only the two Colors/Brushes but also the name(s) of and the check for the clickable column(s).. (Maybe with a e.SubItem.Name.StartsWith("click_"..)

Note that you need to set the Names of the SubItems you want see clickable; there is no SubItemIndex provided in the eventParms. Also note, that you may need to assign that Name in the code, ie when you create/add the ListViewItem.ListViewSubItem; the names I set in the designer somehow didn't stick/work! Also note how you need to qualify ListViewSubItem when you need it!

TaW
  • 53,122
  • 8
  • 69
  • 111