如何获取清单列表中的项目索引 [英] How to get index of item in checkedlistbox
问题描述
foreach(Object item in checkedlistbox.items) { int index=checkedlistbox1.items.indexof(item) }
由于某种原因返回-1.我被建议重写等于任何想法.
for some reason it returns -1. I have been advised to override equals any idea how.
推荐答案
当我尝试使用最小的CheckedListBox时(三个字符串,一个",两个"和三个" "):
When I try it with a minimal CheckedListBox (three strings, "One", "Two" and "Three"):
foreach (Object item in checkedListBox1.Items)
{
int index = checkedListBox1.Items.IndexOf(item);
Console.WriteLine("{0}:{1}", item, index);
}
它符合我的期望:
One:0
Two:1
Three:2
您的代码和我的代码有什么不同?您正在使用哪种对象?您是否考虑过将item
强制转换为正确的类并获取IndexOf
那个?
我正在尝试使用setitemchecked方法以编程方式检查标记上的项目.例如,如果检查了表单加载并假设我没有索引值,但我知道其字符串值,则检查一和三."
如果只需要每个索引,为什么要使用foreach
循环?为什么不直接使用索引?
What is different between your code and mine? What kind of objects are you using? Have you considered casting the item
to the correct class and getting the IndexOf
that?
"Am trying to programatically check the items at the indicies using setitemchecked method. E.g one and three be checked if for example form loads and assume i do not their index values but i know their string values."
If you just want the indexes of each, why use a foreach
loop? Why not use the indexes directly?
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
object item = checkedListBox1.Items[i];
Console.WriteLine("{0}:{1}", item, i);
}
还是我缺少什么?
CheckedListBox.CheckedItems
集合显示了所有当前选中的项目.
The CheckedListBox.CheckedItems
collection shows all currently checked items.
您好,
我希望您可以从下面的代码中获得逻辑.
Hello,
I am hoping you can get the logic for from below code.
foreach (ListItem item in ((CheckBoxList)Form.FindControl("cblActivities")).Items)
{
if (item.Selected)
{
string Activities += item.Value;
}
}
Response.Write(Activities);
谢谢
sanjeev
thanks
sanjeev
这篇关于如何获取清单列表中的项目索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!