7

How can I change the selection color on a ListView? By default, when the user selects an item it shows a blue background. I want to change this to dark gray, or something... Thanks for the help!

Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
Joey Morani
  • 25,431
  • 32
  • 84
  • 131

3 Answers3

8

If you wanted your ListView to have the style of the Windows Explorer ListView (including the nice appearance with rounded edges in Win7/Vista), you could use a little P/Invoke to accomplish that:

[DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList);

// You can subclass ListView and override this method
protected override void OnHandleCreated(EventArgs e)
{
    base.OnHandleCreated(e);
    SetWindowTheme(this.Handle, "explorer", null);
}
Zach Johnson
  • 23,678
  • 6
  • 69
  • 86
3

Well for WinForms you have to set the OwnerDraw property to true and then use the DrawItem and DrawSubItem events to draw the item manually.

See here for an example.

Sam Mackrill
  • 4,004
  • 8
  • 35
  • 55
AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70
1

ObjectListView -- a wrapper around a WinForm ListView -- has properties to let you control the background and foreground color of the selected rows. It uses the technique that Obalix suggested, but it has already done the hard work for you.

So, with a little effort, you can produce something like this:

alt text
(source: codeproject.com)

The "Feel Good Inc" row show a custom foreground and background for selection.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Grammarian
  • 6,774
  • 1
  • 18
  • 32
  • 2
    `ObjectListView` is not a drop-in replacement for `ListView`. Some refactoring of existing code may well be required to use it. – Steve Aug 09 '11 at 10:45
  • I analized this project. But i think it is too hard to learn. –  Nov 10 '12 at 19:01