views:

433

answers:

4

Hello. 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!

A: 

Here's an example to do this in WPF

As far as WinForms Listviews go, I don't think they're flexable enough to do what you want, but here is a custom made listview that adds a lot of new features, including changing the colors of rows. He has several examples on that same page showing how to use his control.

Joel
Oh sorry. It's a Windows Form. :)
Joey Morani
+2  A: 

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.

Obalix
+2  A: 

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
+1  A: 

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

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

Grammarian