解决方法
您必须覆盖
Drawitem
事件并将
DrawMode
属性设置为
DrawMode.OwnerDrawFixed
检查这个样本
- private void listBox1_DrawItem(object sender,DrawItemEventArgs e)
- {
- if (e.Index<0) return;
- //if the item state is selected them change the back color
- if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
- e = new DrawItemEventArgs(e.Graphics,e.Font,e.Bounds,e.Index,e.State ^ DrawItemState.Selected,e.ForeColor,Color.Yellow);//Choose the color
- // Draw the background of the ListBox control for each item.
- e.DrawBackground();
- // Draw the current item text
- e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),Brushes.Black,StringFormat.GenericDefault);
- // If the ListBox has focus,draw a focus rectangle around the selected item.
- e.DrawFocusRectangle();
- }