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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15003095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:44:45  来源:igfitidea点击:

Getting value of selected item in list box as string

c#winformslistbox

提问by Amrit Sharma

I am trying to get the value of the selected item in the listbox using the code below, but it is always returning null string.

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

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

Here I am trying to pass the value of selected item as string to method searchforPrice to retrive dataset from the database.

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

How can i retrive the value of selected item as string?

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

I am adding items to listbox from combo box which in turn loads the items from the database.

我正在将项目从组合框添加到列表框,然后从数据库中加载项目。

 listBox1.Items.Add(comboBox2.Text);

enter image description here

在此处输入图片说明

Anybody has answer for this..

任何人都有这个答案..

采纳答案by Thomas Levesque

If you want to retrieve the display text of the item, use the GetItemTextmethod:

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

string text = listBox1.GetItemText(listBox1.SelectedItem);

回答by Sanu Uthaiah Bollera

If you want to retrieve the item selected from listbox, here is the code...

如果要检索从列表框中选择的项目,这里是代码...

String SelectedItem = listBox1.SelectedItem.Value;

回答by Pir Fahim Shah

If you are using ListBox in your application and you want to return the selected value of ListBox and display it in a Label or any thing else then use this code, it will help you

如果您在应用程序中使用 ListBox 并且想要返回 ListBox 的选定值并将其显示在 Label 或任何其他东西中,则使用此代码,它将帮助您

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

回答by Mahfuzur Rahman

If you want to retrieve your value from an list box you should try this:

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

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

回答by benoit

To retreive the value of all selected item in à listbox you can cast selected item in DataRowView and then select column where your data is:

要检索列表框中所有选定项目的值,您可以在 DataRowView 中投射选定项目,然后选择数据所在的列:

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

回答by Alex

Get FullName in ListBox of files (full path) list (Thomas Levesque answer modificaton, thanks Thomas):

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

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

回答by Zael

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

回答by Vishal Tank

You can Use This One To get the selected ListItme Name ::

您可以使用 This One 来获取选定的 ListItme Name ::

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

Make sure that Your each ListBoxItem have a Name property

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

回答by Chris Hymanson

The correct solution seems to be:

正确的解决方案似乎是:

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

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

Please be sure to use .Contentand not .Name.

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

回答by Davide C

Elaborating on previous answer by Pir Fahim, he's right but i'm using selectedItem.Text (only way to make it work to me)

详细说明 Pir Fahim 之前的回答,他是对的,但我正在使用 selectedItem.Text (让它对我有用的唯一方法)

Use the SelectedIndexChanged() event to store the data somewhere. In my case, i usually fill a custom class, something like:

使用 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);
}

And then you can retrieve the rest of data from a List of "myItems"..

然后您可以从“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;
}