简体   繁体   中英

C# check if selected items in a listbox are in a sequence

我有一个包含 29 个项目的列表框,我想检查用户是否按顺序选择了项目,例如他是否选择 (1,2,3,4) 一个消息框会显示他选择了 (1 到 4) 和如果他选择 (1,3,5) 消息框会说他选择了 (1,3 和 5)。

I see what you want to do. The first approach would be to know what he selected like this :

for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
  MessageBox.Show("You selected the item " + listBox1.SelectedItems[i] + " with index " + i.ToString());
}

And then, as saw in the MessageBox, get the index which I think is what you want. If you want to list all selected items in one MessageBox , just let the user select the items he want, make a button that will say what items he selected in total .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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