Way to color parts of the Listbox/ListView line in C# WinForms?(在 C# WinForms 中为 Listbox/ListView 行的部分着色的方法?)
问题描述
- 有没有办法为 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 行的部分着色的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C# WinForms 中为 Listbox/ListView 行的部分着色的方法?


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01