1

Let's say that I have a ListView and a TextBox. I click on the first item in the ListView, so that it becomes selected. I then click in the TextBox. What happens now is that the ListView becomes inactive, and therefore the selected item background becomes light gray, and not the sharp blue color it had when active.

My question is if it's possible to make the selection in the Listview to always have that blue background color. I think I recollect a ListView property for this, but I can't remember what it is...

5
  • This has been solved here stackoverflow.com/questions/5179664/…
    – Moti Azu
    Dec 25, 2014 at 15:24
  • Set HideSelection to false!
    – TaW
    Dec 25, 2014 at 15:31
  • I have HideSelection to False already.
    – spunit
    Dec 25, 2014 at 15:32
  • I looked at the link, but I would rather not use background colors if it's possible without. But maybe it's the only way...
    – spunit
    Dec 25, 2014 at 15:37
  • Well, thanks anyway.
    – spunit
    Dec 25, 2014 at 16:53

1 Answer 1

2

This might get you started.

First, set OwnerDraw to true and HideSelection to False. Then, we need to fiddle with some events. OwnerDraw can be a chore because it means you have to handle all the item, subitem and column header drawing. Fortunately, for what you want it is not too bad:

// do the default
private void ListView1_DrawColumnHeader(object sender, 
     DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

Since Item.Text is also SubItem(0), you can skip DrawItem and just respond to DrawSubItem:

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    if (!object.ReferenceEquals(this.ActiveControl, listView1) && (e.ItemIndex != -1)) 
    {
        // ToDo offer an early exit if HideSelection is false (?)
        if (e.Item.Selected) 
        {
            e.Graphics.FillRectangle(new SolidBrush(SystemColors.Highlight), 
                                     e.Bounds);

            TextRenderer.DrawText(
                      e.Graphics, " " + listView1.Items(e.ItemIndex).SubItems(e.ColumnIndex).Text, 
                      listView1.Font, e.Bounds, 
                      SystemColors.HighlightText, SystemColors.Highlight, 
                      TextFormatFlags.Left | TextFormatFlags.VerticalCenter
                      );

             // default method seems off a little
            //e.DrawText(TextFormatFlags.Left Or TextFormatFlags.VerticalCenter)
        } else {
            e.DrawDefault = true;
        }
    } else {
        e.DrawDefault = true;
    }
}

Notes:

I didnt notice this was tagged C# until after I coded it, so the translation from VB might be off a little here and there. It fills in a slightly larger rectangle than the default paint action will. I'll recheck it tomorrow when I am less hungry.

As is, it uses the default SystemColors. Since, it doesnt have the focus, I thought it might work to lighten the back color a bit so it does look different focused vs not. See if you like the result:

Private Function LightenColor(clr As Color, amt As Double) As Color
    Dim R, G, B As Integer

    R = Convert.ToInt32(Math.Min(255, clr.R + 255 * amt))
    G = Convert.ToInt32(Math.Min(255, clr.G + 255 * amt))
    B = Convert.ToInt32(Math.Min(255, clr.B + 255 * amt))

    Return Color.FromArgb(clr.A, R, G, B)

End Function

After you convert to C#, use it to define a new lighter selected color:

// small values make a BIG difference
Color InActiveHighlight = LightenColor(SystemColors.Highlight, 0.15);

Then use InActiveHighlight for the back color param to FillRectangle and to TextRenderer. Results:

enter image description here

It is not too noticeable, but the normal paint leaves a little gutter of 2-3px on the left.

I am not too sure lightening is a good idea. First, any lighter and the default text color wont have enough contrast; second it could make it more -- not less -- noticeable on a dark theme; and finally, if SystemColors.Highlight is good enough for a ListBox when not active, it should be good enough for ListView.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.