天天@BLOG

脖子越来越疼,脑袋越来越钝
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ListBox的各行以不同的颜色显示

Posted on 2005-12-29 13:55  天天在线  阅读(486)  评论(0)    收藏  举报

this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;//启用自己Draw模式

private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Brush br;

int r;
Math.DivRem(e.Index,2,out r);
if(r == 0)
br = Brushes.Red;
else
br = Brushes.Blue;
e.DrawBackground();
if((e.State & DrawItemState.Selected) == DrawItemState.Selected)
e.DrawFocusRectangle();

Brush b = Brushes.Black;
e.Graphics.FillRectangle(br,e.Bounds);
e.Graphics.DrawString(this.listBox1.Items[e.Index].ToString(),this.listBox1.Font,b,e.Bounds);

}

@忙碌,不代表有效率;方法,远胜于苦干