问题描述
- 有没有办法为 ListBox 项的部分着色(不仅是整行)?例如,列表框项目由 5 个单词组成,并且只有一个是彩色的或 5 个中的 3 个.
- 有没有办法对 ListView 做同样的事情?(我知道 ListView 可以按列着色,但我想在一列中有多种颜色).
我只对免费的解决方案感兴趣,并且更喜欢它们不会繁重地实施或改变当前的使用(引入彩色 ListBox 来代替普通的最好的工作量最少).
I am interested in only free solutions, and preferred that they are not heavy to implement or change current usage (the least effort to introduce colored ListBox in place of normal one the better).
关于,
疯子
推荐答案
本文 讲述如何使用 DrawItem 的 ListBox 的 DrawMode 设置为 OwnerDraw 值之一.基本上,你可以这样做:
This article tells how to use DrawItem of a ListBox with DrawMode set to one of the OwnerDraw values. Basically, you do something like this:
listBox1.DrawMode = OwnerDrawFixed;
listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
// TODO: Split listBox1.Items[e.Index].ToString() and then draw each separately in a different color
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold),new SolidBrush(color[e.Index]),e.Bounds);
}
代替单个 DrawString 调用,将 listBox1.Items[e.Index].ToString() 拆分为单词并为每个单词单独调用 DrawString.您必须将 e.bounds 替换为 x,y 位置或每个单词的边界矩形.
Instead of the single DrawString call, Split listBox1.Items[e.Index].ToString() into words and make a separate call to DrawString for each word. You'll have to replace e.bounds with an x,y location or a bounding rectangle for each word.
同样的方法应该适用于 ListView.
The same approach should work for ListView.
这篇关于在 C# WinForms 中为 Listbox/ListView 行的部分着色的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!