listbox显示字体的颜色

lsd123 2008-10-06 10:28:29
我想要显示这样的效果:
listbox1.Items.Add("成功"); 要求成功字体的颜色为蓝色
listbox1.Items.Add("失败"); 要求失败字体的颜色为红色
能否作到?
...全文
1069 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
googlg 2012-08-13
  • 打赏
  • 举报
回复
这样弄:ListViewItem li = new ListViewItem();
li.ForeColor = Color.Red;

怎么不行呢?》?????
lsd123 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 NowtAngell 的回复:]
.......
.又来晚
你在事件里面判断 .TEXT 不就完了么?
你选中后的TEXT.
如果是添加.就判断你传递进去的值..
[/Quote]

没来晚,能否详细点
h_w_king 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lsd123 的回复:]
To ericzhangbo1982111

你的方法会可行点,因为可以设置奇,偶行颜色不一样

也谢谢周公,

但能否实现以下显示:

listbox1.Items.Add("第一次成功"); 要求成功字体的颜色为蓝色,第一次默认不变
listbox1.Items.Add("第一次失败"); 要求失败字体的颜色为红色,第一次默认不变
[/Quote]

将"第一次成功 "拆成 " 第一次" 和"成功".
再用e.Graphics.DrawString分别用不同的色彩写出.
NowtAngell 2008-10-06
  • 打赏
  • 举报
回复
.......
.又来晚
你在事件里面判断 .TEXT 不就完了么?
你选中后的TEXT.
如果是添加.就判断你传递进去的值..
lsd123 2008-10-06
  • 打赏
  • 举报
回复
To ericzhangbo1982111

你的方法会可行点,因为可以设置奇,偶行颜色不一样

也谢谢周公,

但能否实现以下显示:

listbox1.Items.Add("第一次成功"); 要求成功字体的颜色为蓝色,第一次默认不变
listbox1.Items.Add("第一次失败"); 要求失败字体的颜色为红色,第一次默认不变

lsd123 2008-10-06
  • 打赏
  • 举报
回复
谢谢关注

还没有我想要的显示
warrior 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 h_w_king 的回复:]
用DrawItem自己画.
参考:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{

string s=this.listBox1.Items[e.Index].ToString();
Brush b;
switch (s)
{
case "成功":
b = new SolidBrush(Color.Blue);
break;
case "失败":

[/Quote]
同意这种方法,ListItem没有ForeColor这个属性。

如果是WebForm,可以在Text中内置HTML,也可以设置ListItem的Attributes,在ListItem的Tag中加入属性。
lsd123 2008-10-06
  • 打赏
  • 举报
回复
To rqx110
谢谢
我补充下我要的结果:
listbox1.Items.Add("第一次成功"); 要求成功字体的颜色为蓝色,第一次默认不变
listbox1.Items.Add("第一次失败"); 要求失败字体的颜色为红色,第一次默认不变


设置item的forecolor属性,是不是整个item都一样
ericzhangbo1982111 2008-10-06
  • 打赏
  • 举报
回复
设置listbox.DrawMode属性为OwnerDrawFixed
然后添加listbox到drawitem事件
在里面选择哪个item颜色自己设置
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
Brush myBrush = Brushes.Black;

// Determine the color of the brush to draw each item based on the index of the item to draw.
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}

// Draw the current item text based on the current Font and the custom brush settings.
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();

}
周公 2008-10-06
  • 打赏
  • 举报
回复
字节写DrawItem方法。

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
listBox1.DrawMode = DrawMode.OwnerDrawFixed;//设置ListBox中每一项都手动绘制
listBox1.Items.Add("成功");
listBox1.Items.Add("失败");
listBox1.Items.Add("事业");
listBox1.Items.Add("成仁");
}
//绘制ListBox项的方法
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
e.DrawBackground();
Brush myBrush = Brushes.Black;

switch (listBox1.Items[e.Index].ToString())
{
case "成功":
myBrush = Brushes.Blue;
Console.WriteLine(listBox1.Items[e.Index].ToString());
break;
case "失败":
myBrush = Brushes.Red;
Console.WriteLine(listBox1.Items[e.Index].ToString());
break;
default:
myBrush = Brushes.Purple;
Console.WriteLine(listBox1.Items[e.Index].ToString());
break;
}
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();

}
}
h_w_king 2008-10-06
  • 打赏
  • 举报
回复
用DrawItem自己画.
参考:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{

string s=this.listBox1.Items[e.Index].ToString();
Brush b;
switch (s)
{
case "成功":
b = new SolidBrush(Color.Blue);
break;
case "失败":
b = new SolidBrush(Color.Red);
break;
default:
b = new SolidBrush(this.ForeColor);
break;
}
e.Graphics.DrawString(s, this.Font, b, e.Bounds);

}
rqx110 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lsd123 的回复:]
引用 2 楼 sleep0110 的回复:
winfrom

ForeColor属性可以更改


winfrom的

如果用ForeColor属性,只能用一种颜色吧?
[/Quote]


设置item的forecolor属性,而不是Listbox的forecolor属性
lsd123 2008-10-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sleep0110 的回复:]
winfrom

ForeColor属性可以更改
[/Quote]

winfrom的

如果用ForeColor属性,只能用一种颜色吧?

睡神在睡觉 2008-10-06
  • 打赏
  • 举报
回复
winfrom

ForeColor属性可以更改
shalen520 2008-10-06
  • 打赏
  • 举报
回复
web or winform?
zero8500 2008-10-06
  • 打赏
  • 举报
回复
JF
zero8500 2008-10-06
  • 打赏
  • 举报
回复
强悍
zgke 2008-10-06
  • 打赏
  • 举报
回复

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();

string StarText=listBox1.Items[e.Index].ToString();
string EndText = StarText.Remove(0, 3);
e.Graphics.DrawString(StarText.Substring(0, 3), e.Font, Brushes.Black, e.Bounds);

switch (EndText)
{
case "成功":
e.Graphics.DrawString(" " + StarText.Remove(0, 3), e.Font, Brushes.Red, e.Bounds);
break;
case "失败":
e.Graphics.DrawString(" " + StarText.Remove(0, 3), e.Font, Brushes.Blue, e.Bounds);
break;

default:
e.Graphics.DrawString(" " + StarText.Remove(0, 3), e.Font, Brushes.Black, e.Bounds);
break;

}



e.DrawFocusRectangle();


}




把字符分两次绘制 前面加空格
王集鹄 2008-10-06
  • 打赏
  • 举报
回复
public struct KeyColor
{
public string key;
public Color color;
public KeyColor(string key, Color color)
{
this.key = key;
this.color = color;
}
}
王集鹄 2008-10-06
  • 打赏
  • 举报
回复
分层绘制,先绘制背景,再绘制各层关键词颜色,参考如下代码:
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("第一次成功、第二次失败");
listBox1.Items.Add("第三次失败。");
listBox1.Items.Add("第四次未知");
}

private KeyColor[] keyColors = new KeyColor[] {
new KeyColor("成功", Color.Blue),
new KeyColor("失败", Color.Red),
new KeyColor("未知", Color.Green) };

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Brush brush = new SolidBrush(e.ForeColor);
string itemText = listBox1.Items[e.Index].ToString();
string backText = itemText;
foreach (KeyColor keyColor in keyColors)
backText = backText.Replace(keyColor.key, new string(' ', keyColor.key.Length));
if (string.Compare(itemText, backText) == 0)
e.Graphics.DrawString(itemText, e.Font, brush, e.Bounds, StringFormat.GenericDefault);
else
{
foreach (KeyColor keyColor in keyColors)
{
string markText = itemText.Replace(keyColor.key, new string(' ', keyColor.key.Length));
string foreText = string.Empty;
for (int i = 0; i < itemText.Length; i++)
foreText += markText[i] == ' ' ? itemText[i] : ' ';
e.Graphics.DrawString(foreText, e.Font,
new SolidBrush(keyColor.color), e.Bounds, StringFormat.GenericDefault);
}
e.Graphics.DrawString(backText, e.Font, brush, e.Bounds, StringFormat.GenericDefault);
}
e.DrawFocusRectangle();
}


不过,不能加入英文,半角字符会导致不能对齐。
完美的方法是分块绘制,需计算每块绘制的长度。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧