获取列表框中所选项的值作为字符串

36

我正在尝试使用以下代码获取列表框中所选项目的值,但它总是返回空字符串。

DataSet ds = searchforPrice(Convert.ToString(listBox1.SelectedItem));

我在这里尝试将所选项目的值作为字符串传递给searchforPrice方法,以从数据库中检索数据集。

如何将所选项目的值作为字符串检索?

我正在从组合框向列表框添加项目,而组合框则从数据库加载项目。

 listBox1.Items.Add(comboBox2.Text);

enter image description here

有人针对这个问题有答案吗?


2
请发布加载列表框的代码。 - Mike Perrenoud
11个回答

101
如果您想获取项目的显示文本,请使用GetItemText方法:
string text = listBox1.GetItemText(listBox1.SelectedItem);

@AmritSharma,你有检查过SelectedItem不是空值吗? - Thomas Levesque
@ThomasLevesque,请查看上面的截图。 - Amrit Sharma
GetItemText方法不可用,请帮忙,我正在开发Windows Phone应用程序。 - Kartiikeya
1
@BaruchAtta GetItemText 肯定存在(从ListControl继承)。 我的猜测是您没有使用Windows Forms,而是使用不同的ListBox控件,比如WPF或UWP。 - Thomas Levesque
3
@BaruchAtta,你看到我的评论了吗?你没有使用与我的答案适用的相同UI框架。这个问题是关于Windows表单(在标签中是“winforms”),而我的回答适用于Windows表单,它一定有GetItemText方法。如果你正在使用其他UI框架,比如WPF或UWP,则此答案不适用。你使用VS2017并不能说明你使用的是哪个UI框架,因为有几种存在。 - Thomas Levesque
显示剩余6条评论

12
如果您在应用程序中使用了ListBox并且希望返回ListBox的选定值并将其显示在Label或其他任何位置,则可以使用此代码来实现,它将帮助您。
 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
         label1.Text  = listBox1.SelectedItem.ToString();
    }

我已经尝试了您建议的代码,它输出的字符串是:System.Windows.Controls.ListViewItem: AndThenTheActualString - 如何摆脱冗长的system.windows - 没有人想看到它? - BenKoshy
1
@BKSpurgeon 这个答案适用于 ListBox。你的代码使用的是 ListView,这就是为什么会出现 "System.Windows.Controls.ListViewItem" 的原因。 - Dialecticus

1
string textValue = ((ListBoxItem)listBox1.SelectedItem).Content.ToString();

1
不建议像这样使用 as。虽然看起来更简洁,但是它是格式错误的。as 是用于检查对象是否为特定类型的。你正在使用错误的工具。最好将其正确转换为 ((ListBoxItem)listBox1.SelectedItem) - PC Luddite
感谢您的解释,PC Luddite,我会进行更正。 - Zael

1

要检索列表框中所有选定项的值,您可以将选定项强制转换为DataRowView,然后选择包含数据的列:

foreach(object element in listbox.SelectedItems) {
    DataRowView row = (DataRowView)element;
    MessageBox.Show(row[0]);
}

0

您可以使用以下代码获取所选 ListItme 的名称:

String selectedItem = ((ListBoxItem)ListBox.SelectedItem).Name.ToString();

确保每个ListBoxItem都有一个Name属性


0

在文件列表的ListBox中获取完整路径的FullName(Thomas Levesque的答案修改版,感谢Thomas):

...
        string tmpStr = "";
        foreach (var item in listBoxFiles.SelectedItems)
        {
            tmpStr += listBoxFiles.GetItemText(item) + "\n";
        }
        MessageBox.Show(tmpStr);
...

0
如果您想从列表框中检索值,可以尝试以下方法:
String itemSelected = numberListBox.GetItemText(numberListBox.SelectedItem);

1
GetItemText方法不可用...请帮忙...我正在开发Windows Phone应用程序。 - Kartiikeya

0

对 Pir Fahim 先前的答案进行补充,他是正确的,但我使用 selectedItem.Text(对我来说这是唯一可行的方法)

使用 SelectedIndexChanged() 事件将数据存储在某个位置。 在我的情况下,我通常会填充一个自定义类,类似于:

class myItem {
    string name {get; set;}
    string price {get; set;}
    string desc {get; set;}
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
     myItem selected_item = new myItem();
     selected_item.name  = listBox1.SelectedItem.Text;
     Retrieve (selected_item.name);
}

然后,您可以从“myItems”列表中检索其余数据。

myItem Retrieve (string wanted_item) {
    foreach (myItem item in my_items_list) {
        if (item.name == wanted_item) {
               // This is the selected item
               return item; 
        }
    }
    return null;
}

-1

设置属性 listbox1 的 DisplayMember = "Text";

设置属性 listbox1 的 ValueMember = "Value";

事件

    private void listbox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string selectedValue = listbox1.SelectedValue.ToString();
    }

目前您的回答写得不够清晰。请编辑以添加其他细节,以帮助其他人理解这如何回答所提出的问题。您可以在帮助中心找到有关编写良好答案的更多信息。 - Community
对象引用未设置到对象的实例。 - MindRoasterMir

-1

正确的解决方案似乎是:

string text = ((ListBoxItem)ListBox1.SelectedItem).Content.ToString();

请务必使用.Content而不是.Name


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接