为列表框中的特定项目着色 [英] Coloring a specific item in Listbox

查看:52
本文介绍了为列表框中的特定项目着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来自SQL库的表填充列表框
列为b1,b2,b3,b4,b5,b6,b7
列表框中最多有100万行
示例b1,b2,b3,b4,b5,b6,b7如下:
1,2,3,4,5,6,7
4,5,14,25,35,36,39
14,15,22,23,25,30,31
1,7,14,18,22,26,30

如果任何行包含以下任何数字(1,4,14,23,35,36,37)
该项目应赋予与列表框前景色不同的颜色,如波纹管

1 ,2,3, 4 ,5,6,7
4 ,5, 14 ,25, 35 36 ,39
14 ,15,22, 23 ,25,30,31
1 ,7, 14 ,18,22,26,30

解决方案

除非您实现列表框控件的自定义版本,否则我不确定是否可以在列表框中进行此操作.就我所知,列表框无法区分列表中每个项目的子项".即在我们的情况下,一个项目将是列表框中的每一行,一个子项目将是该行中的每个数字.

如果可以的话,可以使用ListView控件替换列表框,该控件将允许访问每个项目的SubItem成员.

请记住,将ListView.View属性设置为Details才能使用此示例.

下面的代码适用于C#,但我想可以轻松地将其移植到VB :)

ListView listView = new ListView();

listView.View = View.Details;



ListViewItem lvi = new ListViewItem();

// individual format for each subitem
lvi.UseItemStyleForSubItems = false;

// create and add subitems with unique formats
ListViewItem.ListViewSubItem lviSubItem1 =
    new ListViewItem.ListViewSubItem();
lviSubItem1.Text = "1";
lviSubItem1.ForeColor = Color.Green;

lvi.SubItems.Add(lviSubItemX);
ListViewItem.ListViewSubItem lviSubItem2 =
    new ListViewItem.ListViewSubItem();
lviSubItem2.Text = "2";
lviSubItem2.ForeColor = Color.Red;
lvi.SubItems.Add(lviSubItem2);

ListViewItem.ListViewSubItem lviSubItem3 =
    new ListViewItem.ListViewSubItem();
lviSubItem3.Text = "3";
lviSubItem3.ForeColor = Color.Blue;
lvi.SubItems.Add(lviSubItem3);

// add the subitem to underlying list of items
listView.Items.Add(lvi);



希望这会有所帮助,如果不是这样的话,也许有人会在列表框中完成此操作,但是除了创建自定义列表框之外,我想不出任何方法来做到这一点:)

戴夫


我认为唯一的方法是提出一个自定义控件,该控件衍生自ListBox控件,该控件使用RichText编辑框显示其项.

另一方面,您可以更改项目的背景颜色,使其不包含一个或多个指定值,并且在每个项目的工具提示中,可以显示列表框项目中包含哪些数字.

I fill the listbox with tables from SQL base
The colums are b1,b2,b3,b4,b5,b6,b7
There are up to 1 million rows in listbox
example b1,b2,b3,b4,b5,b6,b7 are as follows:
1,2,3,4,5,6,7
4,5,14,25,35,36,39
14,15,22,23,25,30,31
1,7,14,18,22,26,30

If any row contain any of this numbers(1,4,14,23,35,36,37)
that item should given a different color from listbox''s forecolor as bellow

1,2,3,4,5,6,7
4,5,14,25,35,36,39
14,15,22,23,25,30,31
1,7,14,18,22,26,30

Thanks for your help?

解决方案

I am not sure if it is posible to do this in a listbox unless you implement a custom version of the listbox control. A listbox (as far as I know) cannot distinguish between "subitems" for each item in the list. i.e an item in our case would be each row in the listbox, a subitem would be each number in the row.

If possible by your design, you can replace the listbox with a ListView control that will allow access into SubItem members for each item.

Remember to set the ListView.View property to Details to use this example.

Code below is for C#, but I guess can easily be ported to VB :)

ListView listView = new ListView();

listView.View = View.Details;



ListViewItem lvi = new ListViewItem();

// individual format for each subitem
lvi.UseItemStyleForSubItems = false;

// create and add subitems with unique formats
ListViewItem.ListViewSubItem lviSubItem1 =
    new ListViewItem.ListViewSubItem();
lviSubItem1.Text = "1";
lviSubItem1.ForeColor = Color.Green;

lvi.SubItems.Add(lviSubItemX);
ListViewItem.ListViewSubItem lviSubItem2 =
    new ListViewItem.ListViewSubItem();
lviSubItem2.Text = "2";
lviSubItem2.ForeColor = Color.Red;
lvi.SubItems.Add(lviSubItem2);

ListViewItem.ListViewSubItem lviSubItem3 =
    new ListViewItem.ListViewSubItem();
lviSubItem3.Text = "3";
lviSubItem3.ForeColor = Color.Blue;
lvi.SubItems.Add(lviSubItem3);

// add the subitem to underlying list of items
listView.Items.Add(lvi);



Hope this helps and if not maybe someone will have a way to do it in listbox, but apart from making a custom listbox I can''t think of any way to do it :)

Dave


I think the only way to do this is to come up with a custom control derived from the ListBox control that uses a RichText edit box to display its items.

On the other hand, you could just change the background color of an item than contains one or more of the specified values, and in a tooltip for each item, you could show which of numbers are contained in the list box item.


这篇关于为列表框中的特定项目着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆