简体   繁体   English

通过循环从列表框中选择项目?

[英]Select items from a listbox through a loop?

I want to make it so that through a loop, it will select an item from the listbox. 我想这样做,以便通过循环从列表框中选择一个项目。 I was thinking about doing a for loop. 我正在考虑做一个for循环。 This is (basically) what I want to accomplish: (基本上)这是我要完成的工作:

for (int i = 0; i < lbRooms.Items.Count; i++)
{
    lbRooms.Items.Select(i);
    // do stuff here with the selected item
}

I know thats not how it works, but I want it to do it like that. 我知道那不是它的工作方式,但是我希望它那样做。 I appreciate all the help, thanks =D 我感谢所有帮助,谢谢= D

EDIT: I think this will work, but I'm sure it can be improved: 编辑:我认为这将工作,但我敢肯定它可以改善:

for (int i = 0; i < lbRooms.Items.Count; i++)
{
    lbRooms.SetSelected(i, true);
}

Try: 尝试:

lbRooms.setSelected(i, true);

instead of: 代替:

lbRooms.Items.Select(i);
foreach (listitem item in lbRooms.Items)
    //do item manipulation here

You can't select your items like that, You can use indexer to get your item: 您不能像这样选择商品,可以使用索引器获取商品:

for (int i = 0; i < lbRooms.Items.Count; i++)
{
   var currentItem = lbRooms.Items[i];
}

If you want to select that item you can set Selected property to true: 如果要选择该项目,可以将Selected属性设置为true:

currentItem.Selected = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM