c# winform listbox 如何 获取 当前 选中的值 急!!!

来源:百度知道 编辑:UC知道 时间:2024/09/22 15:37:03
c# winform listbox 如何 获取 当前 选中的值
有什么属性或方法?又或者有什么办法?

各位朋友能否给点代码参考或实现思路?

在线等!
我已经写了很多事件了!窗体一次比一次显示的慢!

非得用事件吗? 可以不用吧?

3楼也夸张了吧...一个界面上有那么多listbox如果每个都写循环,不崩掉就奇怪了!(巨卡无比!)

2楼说的3个属性我都用过了!

我是这样写的: String ParmValue = (string)this.listKehuliebiaomingcheng.SelectedItem; throw new Exception(ParmValue);
在这个语句前抛出异常捕获不了,但在这句话后面写个异常就能捕捉...

程序每到这里就几乎崩溃!所以我就在这里被郁闷到了...

可以呀~
我在listbox外放了个按钮,点击按钮显示选中行的值

//循环listbox所有行
for (int i = 0; i < listBox1.Items.Count; i++)
{
//行被选中时
if (listBox1.SelectedItems.Contains(listBox1.Items[i]))
{
MessageBox.Show(listBox1.Items[i].ToString());
}
}
这样就检测出你选择了哪一行,并显示它的文本

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(listBox1.SelectedItem.ToString());
}

listBox1.SelectedItem
listBox1.SelectedValue
listBox1.SelectedIndex
都可以啊