C#WinForm ListBox设置文字颜色
给ListBox的DrawItem方法绑定事件 drawLog//给ListBox的DrawItem事件绑定一个方法,取名drawProcessInfoLB_ProcessInfo.DrawItem += new DrawItemEventHandler(drawProcessInfo);
定义drawProcessInfo方法
{if (LB_ProcessInfo.Items.Count > 0 && LB_ProcessInfo.Items.Count > e.Index){if (e.Index < 0){ return; }string str = LB_ProcessInfo.Items[e.Index].ToString();Color foreColor = Color.Black;//重点,根据item的具体内容决定颜色//if (str.Contains("本次测试PASS")) { foreColor = Color.Green; }if (str.Contains("异常")) { foreColor = Color.Red; }if (str.Contains("Message")) { foreColor = Color.Red; }if (str.Contains("失败")) { foreColor = Color.Red; }if (str.Contains("重新启动软件")) { foreColor = Color.Red; }//重点,画上颜色e.Graphics.DrawString(str, e.Font, new SolidBrush(foreColor), e.Bounds);}}
ListBox的 DrawMode属性设置为 OwnerDrawFixed或者OwnerDrawVariable,才能让DrawItem事件触发效果如下:
转载:/u_15242344/2842174