分享

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

 goodwangLib 2017-10-03
复制代码
        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); } }
复制代码

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约