首页 > Windows开发 > 详细

c# 实现 ListBox 每一列可以有各自的颜色

时间:2017-05-05 18:19:57      阅读:444      评论:0      收藏:0      [点我收藏+]

技术分享

上图是想实现的效果, 注意不是所有行一个颜色 

 

首先把  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 每一列可以有各自的颜色

原文:http://www.cnblogs.com/lthxk-yl/p/6814242.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!