显示标签的项目列表。 [英] Displaying the list of items to the label.
问题描述
大家好,
我正在使用清单来查明列表中选择了多少项。所以我使用数组列表来收集项目,但无法在标签中显示。请建议我这样做。
Hello all,
I am working with checklist to find out how many items are selected in the list. So I am using array List to collect the items but unable to display in the label. Please suggest me to do this.
ArrayList alist=new ArrayList();
for (int i = 0; i < chkList.Items.Count; i++)
{
if (chkList.Items[i].Selected)
{
alist.Add(chkList.Items[i].Value);
}
}
lbl.Text = alist;
//这里将列表项分配给标签
输出:
System.ArrayList.Collections
// Here assigning the list items to label
OUTPUT:
System.ArrayList.Collections
推荐答案
如果您想在标签中显示checkboklist的所有选中项目,请尝试以下代码:
If You want to show all the checked item of checkboklist in label than try below code:
ArrayList alist = new ArrayList();
for (int i = 0; i < chkList.Items.Count; i++)
{
if (chkList.Items[i].Selected)
{
alist.Add(chkList.Items[i].Value);
label.text = label.text + "," + chkList.Items[i].Text; // add this line in your code
}
}
这里唯一的问题就是这条线
The only problem here is with this line
lbl.Text = alist;
因为arraylist是一个集合所以,你将不得不遍历列表
Since, arraylist is a collection so, you will have to iterate through the list
lbl.Text="";
foreach(string value in alist)
{
lbl.Text = lbl.Text + value;
}
我假设你在arraylist中存储字符串值。如果有任何其他数据类型,请进行正确的类型转换。
I am assuming you are storing string values in arraylist. If any other data type, please do proper type cast.
通过LINQ:
By means of LINQ:
var itemQuery = from e in chkList.Items.Cast<ListItem>() where e.Selected select e.Text;
lbl.Text = string.Join(", ", itemQuery);
干杯
Andi
Cheers
Andi
这篇关于显示标签的项目列表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!