c# – 如何更改ListBox选择背景颜色?

前端之家收集整理的这篇文章主要介绍了c# – 如何更改ListBox选择背景颜色?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
默认情况下,使用默认颜色的 Windows设置是蓝色的.
假设我想把它永久改成红色.我使用Winforms.

提前致谢.

解决方法

您必须覆盖 Drawitem事件并将 DrawMode属性设置为 DrawMode.OwnerDrawFixed

检查这个样本

  1. private void listBox1_DrawItem(object sender,DrawItemEventArgs e)
  2. {
  3. if (e.Index<0) return;
  4. //if the item state is selected them change the back color
  5. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
  6. e = new DrawItemEventArgs(e.Graphics,e.Font,e.Bounds,e.Index,e.State ^ DrawItemState.Selected,e.ForeColor,Color.Yellow);//Choose the color
  7.  
  8. // Draw the background of the ListBox control for each item.
  9. e.DrawBackground();
  10. // Draw the current item text
  11. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),Brushes.Black,StringFormat.GenericDefault);
  12. // If the ListBox has focus,draw a focus rectangle around the selected item.
  13. e.DrawFocusRectangle();
  14. }

猜你在找的C#相关文章