c# listbox 获取选中项

//可以显示的用法
string abc = string.Format("下标:{0}, 选中:{1}", listBox1.SelectedIndex, listBox1.SelectedItem);
MessageBox.Show(abc);
//下面的为什么不能,我想直接点就取得选中的值
MessageBox.Show(listBox1.SelectedIndex,listBox1.SelectedItem);

MessageBox.Show(listBox1.SelectedIndex,listBox1.SelectedItem);
这样写不编译不过的。因为MessageBox.Show方法没有int型的重载方法。
并且你这么写也达不到你想要的目的,listBox1.SelectedIndex会显示弹出窗口的内容里,listBox1.SelectedItem会显示在弹出窗口的标题上面。

如果非要这么就显示的做字符串转换
MessageBox.Show(listBox1.SelectedIndex.ToString(),listBox1.SelectedItem.ToString());

想写在一起显示可改成
MessageBox.Show(listBox1.SelectedIndex.ToString() + listBox1.SelectedItem.ToString());追问

我只想看一下弹出来看下正不正确。之前有试过.tostring,但是后面没有加上(),- - !
想最后确认下,listBox1.SelectedItem,这个直接就是int类型的吗?怎么知道它就是int类型呢?

追答

listBox1.SelectedItem这个不是int型,而是object型。
当你在写listBox1.后,就可以看vs.net弹出的智能感知,看看这个SelectedItem是什么类型的。

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-02-05
语法格式如下:
public static MessageBox.DialogResultShow(string text );

text 必须为 string 类型.
第2个回答  2013-04-02
textBox2.Text = listBox1.Text的
或尝试
textBox2.Text = listBox1.SelectedItem.Text
第3个回答  2013-02-05
MessageBox.Show(listBox1.SelectedIndex,listBox1.SelectedItem.Text);追问

还是不行哦,
刚试了,
//获取选中项可以这样写
MessageBox.Show(listBox1.Text);
选中下标就不知道怎么写了

难道非得写这么长才可以么?
//这个写法是可以的
MessageBox.Show(string.Format("{0}", listBox1.SelectedIndex));

追答

MessageBox.Show(listBox1.SelectedIndex);
不用写那么长

相似回答