Как сделать чтобы при потере фокуса элемент listbox-а перекрашивался в другой цвет?
private void listBox1_MouseLeave(object sender, EventArgs e)
{
// Как перерисовать выделенный элемент?
}
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
bool isItemSelected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
int itemIndex = e.Index;
if (itemIndex >= 0 && itemIndex < listBox1.Items.Count)
{
Graphics g = e.Graphics;
// Background Color
SolidBrush backgroundColorBrush = new SolidBrush((isItemSelected) ? Color.Red : Color.White);
g.FillRectangle(backgroundColorBrush, e.Bounds);
// Set text color
string itemText = listBox1.Items[itemIndex].ToString();
SolidBrush itemTextColorBrush = (isItemSelected)
? new SolidBrush(Color.White)
: new SolidBrush(Color.Black);
g.DrawString(itemText, e.Font, itemTextColorBrush, listBox1.GetItemRectangle(itemIndex).Location);
// Clean up
backgroundColorBrush.Dispose();
itemTextColorBrush.Dispose();
}
e.DrawFocusRectangle();
}