由于自己练习遇到了,所以记录一下。作用是在ListBox中让不同的文字显示不同的颜色,基本代码如下:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
string s = this.listBox1.Items[e.Index].ToString();
if (s.Contains("数据正常"))
{
e.Graphics.DrawString(s, this.Font, Brushes.Green, e.Bounds);
}
else if (s.Contains("数据警告"))
{
e.Graphics.DrawString(s, this.Font, Brushes.Red, e.Bounds);
}
else
e.Graphics.DrawString(s, this.Font, new SolidBrush(this.ForeColor), e.Bounds);
}
注意一个问题,只有代码不行,需要事件关联和属性设置!!!
尤其是DrawItem,这是事件关联,我在这卡了很长时间。。。。。
下边是效果图
就酱紫