C# 将列表框中选定项的值获取为字符串

C# 将列表框中选定项的值获取为字符串,c#,winforms,listbox,C#,Winforms,Listbox,我试图使用下面的代码获取列表框中所选项目的值,但它总是返回空字符串 DataSet ds = searchforPrice(Convert.ToString(listBox1.SelectedItem)); 在这里,我试图将所选项的值作为字符串传递给方法searchforPrice,以从数据库检索数据集 listBox1.Items.Add(comboBox2.Text); 如何将所选项目的值检索为字符串 我从组合框向列表框添加项目,组合框从数据库加载项目 listBox1.Items.

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

DataSet ds = searchforPrice(Convert.ToString(listBox1.SelectedItem));
在这里,我试图将所选项的值作为字符串传递给方法searchforPrice,以从数据库检索数据集

 listBox1.Items.Add(comboBox2.Text);
如何将所选项目的值检索为字符串

我从组合框向列表框添加项目,组合框从数据库加载项目

 listBox1.Items.Add(comboBox2.Text);


任何人对此都有答案。

如果要检索项目的显示文本,请使用以下方法:


如果要检索从列表框中选择的项目,请参阅以下代码

String SelectedItem = listBox1.SelectedItem.Value;

如果您在应用程序中使用ListBox,并且希望返回ListBox的选定值并将其显示在标签或任何其他内容中,那么使用此代码将对您有所帮助

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
         label1.Text  = listBox1.SelectedItem.ToString();
    }

如果要从列表框中检索值 你应该试试这个:

String itemSelected = numberListBox.GetItemText(numberListBox.SelectedItem);

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

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

在文件列表框(完整路径)列表中获取全名(Thomas Levesque answer Modification,谢谢Thomas):


您可以使用此列表获取所选列表名::

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

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

正确的解决方案似乎是:

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


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

详细说明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”列表中检索其余数据


请发布加载列表框的代码。@AmritSharma,您是否检查了
SelectedItem
是否不为空?@Thomaslevsque检查了屏幕截图above@ThomasLevesque,是selectedItem值为null@BaruchAtta
GetItemText
明确(继承自
ListControl
)。我猜您使用的不是Windows窗体,而是WPF或UWP,它们有不同的列表框控件。@BaruchAtta您读过我的评论吗?您使用的UI框架与我的答案不同。问题是关于Windows窗体(
winforms
in标记),这个答案适用于Windows窗体,它肯定有GetItemText方法。如果您正在使用另一个UI框架,如WPF或UWP,则答案不适用。事实上,您使用的VS2017并没有说明您使用的是哪个UI框架,因为有几个exist.GetItemText方法不可用…请帮助…我正在开发windows phone应用程序已尝试了您建议的代码,它输出的字符串是:System.Windows.Controls.ListViewItem:和TheatheticualString-如何摆脱冗长的System.Windows-没有人想看到它?@BKSpurgeon答案与ListBox一起工作。您的代码使用的是ListView,这就是为什么在那里有“System.Windows.Controls.ListViewItem”的原因。listBox1.SelectedItem.Value;//listBox1.SelectedItem的和处的.Value部分似乎没有显示在我的IDE中。有什么提示吗?像这样使用
不是个好主意。它看起来更干净,但它是畸形的
as
用于检查对象是否为特定类型。你在工作中使用了错误的工具。最好将其正确转换为
((ListBoxItem)listBox1.SelectedItem)
感谢PC Luddite的解释,我会更正它。
String selectedItem = ((ListBoxItem)ListBox.SelectedItem).Name.ToString();
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);
}
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;
}