获取列表框中所选项目的文本 [英] Get text of selected items in a ListBox
本文介绍了获取列表框中所选项目的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在消息框中显示 listBox1 的选定项目,代码如下:
I'm trying to show the selected items of listBox1 in a Message Box here's the code:
int index;
string item;
foreach (int i in listBox1 .SelectedIndices )
{
index = listBox1.SelectedIndex;
item = listBox1.Items[index].ToString ();
groupids = item;
MessageBox.Show(groupids);
}
问题是当我选择多个项目时消息框显示我选择的第一个并重复消息例如:如果我选择了 3 个项目,该消息将与第一个项目一起出现 3 次
The problem is that when I select more than one item the message box shows the frist one I've selected and repeats the message EX: if I selected 3 items the message will appear 3 times with the first item
推荐答案
foreach 循环中的 i
有你需要的索引.您使用的 listBox1.SelectedIndex
只有第一个.所以项目应该是:
The i
in the foreach loop has the index you need. You're using listBox1.SelectedIndex
which only has the first one. So item should be:
item = listBox1.Items[i].ToString ();
这篇关于获取列表框中所选项目的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文