I want to change the color of a selected item in a listbox to a specific ARGB value. When it's not selected it's showing the ForeColor I've defined in the Properties of my listBox.
But whatever I try it's either setting the brush to a predefined color (white, green, whatever), changing the color when selected & not selected for both in the same color... or it's not changing at all. The solutions I see on stackoverflow are XAML based, but I'm using Winforms C# .NET, so that's not an option.
I've managed to make a custom listBox already using OwnerDrawFixed as DrawMode and a custom DrawItem predefined like this:
{
SolidBrush myBrushBack = new SolidBrush(Color.FromArgb(255, 42, 42, 42));
SolidBrush myBrushFore = new SolidBrush(Color.FromArgb(255, 62, 182, 86));
if (e.Index < 0) return;
e.DrawBackground();
Graphics g = e.Graphics;
Brush brush = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ?
myBrushBack : new SolidBrush(e.BackColor);
myBrushFore : new SolidBrush(e.ForeColor);
g.FillRectangle(brush, e.Bounds);
SizeF size = e.Graphics.MeasureString(listBoxTracks.ToString(), e.Font);
e.Graphics.DrawString(listBoxTracks.Items[e.Index].ToString(), e.Font,
new SolidBrush(e.ForeColor), e.Bounds.Left + (e.Bounds.Width / 29 - size.Width / 39), e.Bounds.Top + (e.Bounds.Height / 2 - size.Height / 2), StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
This code doesn't give me the right green I want + it changes the selected & not selected text color for both:
e.Graphics.DrawString(listBoxTracks.Items[e.Index].ToString(), e.Font, Brushes.Green, e.Bounds, StringFormat.GenericDefault);
How it is behaving now: https://i.stack.imgur.com/rFUrP.jpg
How I want it to behave: https://i.stack.imgur.com/ZuePQ.jpg