上图是想实现的效果, 注意不是所有行一个颜色
首先把 listbox 的 DrawMode 属性 改为 OwnerDrawFixed
然后 override listbox 的 函数 DrawItem 系统重命名为 listBox1_DrawItem
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground(); //先调用基类实现
if (e.Index < 0) //form load 的时候return
return;
//因为此函数每一个 listItem drawing 都要调用, 所以不能简单的只写e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),e.Font, Brushes.Red, e.Bounds);
//那样会造成所有item一个颜色
//这里是用item字符串是否包含某些词决定的 , 不好
if (listBox1.Items[e.Index].ToString().Contains("error"))
{
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),
e.Font, Brushes.Red, e.Bounds);
}
else if (listBox1.Items[e.Index].ToString().Contains("warn"))
{
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),
e.Font, Brushes.Red, e.Bounds);
}
else
{
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, Brushes.Black, e.Bounds);
}
}
当前文章:c#实现ListBox每一列可以有各自的颜色-创新互联
文章分享:https://www.cdcxhl.com/article16/ccdhdg.html
成都网站建设公司_创新互联,为您提供Google、小程序开发、网站改版、静态网站、网站维护、移动网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联