【发布时间】:2010-09-22 18:04:38
【问题描述】:
喂! 我想在列表框中显示我的日志系统输出,并根据级别或日志条目自定义突出显示。 (一般、警告、错误、调试、跟踪)
somelistbox.Items.Add("Starting"); // I would like to drawn this as grey
somelistbox.Items.Add("Error!"); // I would like to drawn this as red
所以我想添加一个新的东西,比如 typeoflog,但我不知道怎么做。
somelistbox.Items.Add("Error!",Type.Error);
我有这个代码,它为 items 着色,取决于项目编号,但这不是我要找的。p>
private void general_log_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Brush myBrush = Brushes.Black;
switch (actualLogType)
{
case LogTypes.General:
myBrush = Brushes.Black;
break;
case LogTypes.Warning:
myBrush = Brushes.Orange;
break;
case LogTypes.Error:
myBrush = Brushes.Purple;
break;
case LogTypes.Debug:
myBrush = Brushes.AntiqueWhite;
break;
}
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
更新:如果有人还在研究这个,我建议看看 NLog 项目。它有彩色的富文本框目标。
【问题讨论】: