Reputation: 3459
How do you change that ugly blue color that you get when you select something in a listbox
in Windows Forms? All of the solutions I was able to find either include re-creating the whole control, or just using WPF. Is there any way to do it within WinForms?
Upvotes: 3
Views: 9324
Reputation: 2612
Before Editing..(he asks about the ListView not the ListBox)
ObjectListView :
ListView1.UseCustomSelectionColors = true;
ListView1.HighlightBackgroundColor = Color.Red; // for example :)
ListView1.UnfocusedHighlightBackgroundColor = Color.Red;
ListView :
myitem.BackColor = Color.Red;
Upvotes: 4
Reputation: 236318
Set DrawMode
of your listBox to OwnerDrawFixed
and subscribe to DrawItem
event:
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Graphics g = e.Graphics;
Brush brush = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ?
Brushes.Red : new SolidBrush(e.BackColor);
g.FillRectangle(brush, e.Bounds);
e.Graphics.DrawString(listBox.Items[e.Index].ToString(), e.Font,
new SolidBrush(e.ForeColor), e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
You can determine state of drawing item by checking e.State
property of event argument. If state is Selected
, then use whatever brush you like (e.g. Red) to fill item line.
Upvotes: 12