[c#] Getting value of selected item in list box as string

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.

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..

This question is related to c# winforms listbox

The answer is


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:

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

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

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

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

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

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

Make sure that Your each ListBoxItem have a Name property


The correct solution seems to be:

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

Please be sure to use .Content and not .Name.


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

String SelectedItem = listBox1.SelectedItem.Value;

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

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

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

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

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

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

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"..

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;
}