Amrit Sharma
Amrit Sharma

Reputation: 1916

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

Upvotes: 36

Views: 318366

Answers (11)

user19289498
user19289498

Reputation: 1

set properties listbox1 DisplayMember = "Text"; set properties listbox1 ValueMember = "Value";

Event

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

Upvotes: -1

Davide C
Davide C

Reputation: 116

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

Upvotes: 0

Chris Jackson
Chris Jackson

Reputation: 1

The correct solution seems to be:

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

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

Upvotes: -1

Vishal Tank
Vishal Tank

Reputation: 163

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

Upvotes: 0

Zael
Zael

Reputation: 360

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

Upvotes: 1

Alex
Alex

Reputation: 1

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

Upvotes: 0

benoit
benoit

Reputation: 737

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

Upvotes: 1

Mahfuzur Rahman
Mahfuzur Rahman

Reputation: 27

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

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

Upvotes: 0

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10623

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

Upvotes: 12

Sanu Uthaiah Bollera
Sanu Uthaiah Bollera

Reputation: 937

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

String SelectedItem = listBox1.SelectedItem.Value;

Upvotes: -3

Thomas Levesque
Thomas Levesque

Reputation: 292355

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

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

Upvotes: 102

Related Questions