locked
Retriving values from a listbox RRS feed

  • Question

  • How can i retrive a value from a list box?
    Tuesday, June 15, 2010 7:33 AM

Answers

  • Hi Helen Zhou,

    The System.Windows.Forms.ListBox.SelectedItem is a property of System.Object type. So it does not have Value and Text properties in it. For example, If I bind a DataTable to a ListBox control, The ListBox.SelectedItem returns an object of System.Data.DataRowView.

    Only System.Web.ListBox control has the SelectedItem property which returns System.Web.UI.WebControls..ListItem object. The ListItem object has Value and Text properties in it.

    For WinForms ListBox, I think we can use ListBox.Text property to get the Text and ListBox.SelectedValue to get the value specified by the ValueMember property.

    Is my understanding correct?

    • Proposed as answer by Vladimir.Ilic Friday, June 18, 2010 5:58 PM
    • Marked as answer by Helen Zhou Monday, June 21, 2010 7:38 AM
    Friday, June 18, 2010 11:51 AM
  • Hi Dukenathan,

    SelectedValue only returns something sensible if you set the "ValueMember" property...

    If you don't set that property (or can't because the object in the listbox doesn't have a property you want to use), you should use SelectedItem instead...

    So, make sure to set the DataSource of the listbox, instead of adding the items manually, eg:

    Code snippet:

            private void Form1_Load(object sender, EventArgs e)
            {
                List<string> values = new List<string>();
                values.Add("1111");
                values.Add("2222");
                listBox1.DataSource = values;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(listBox1.SelectedValue.ToString());
            }

    And now, the SelectedValue will return you the string representation.

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, June 21, 2010 7:52 AM
    Monday, June 21, 2010 7:52 AM

All replies

  • Hi,

    ListBox.SelectedValue return the selected value of the ValueMember from the DataSource.

    Tuesday, June 15, 2010 8:00 AM
  • Hi Dukenathan,

    Get value:ListBox1.SelectedItem.Value
    Get Text:   ListBox1.SelectedItem.Text

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, June 17, 2010 9:37 AM
  • Hi Helen Zhou,

    The System.Windows.Forms.ListBox.SelectedItem is a property of System.Object type. So it does not have Value and Text properties in it. For example, If I bind a DataTable to a ListBox control, The ListBox.SelectedItem returns an object of System.Data.DataRowView.

    Only System.Web.ListBox control has the SelectedItem property which returns System.Web.UI.WebControls..ListItem object. The ListItem object has Value and Text properties in it.

    For WinForms ListBox, I think we can use ListBox.Text property to get the Text and ListBox.SelectedValue to get the value specified by the ValueMember property.

    Is my understanding correct?

    • Proposed as answer by Vladimir.Ilic Friday, June 18, 2010 5:58 PM
    • Marked as answer by Helen Zhou Monday, June 21, 2010 7:38 AM
    Friday, June 18, 2010 11:51 AM
  • Hi Helen Zhou,

    The System.Windows.Forms.ListBox.SelectedItem is a property of System.Object type. So it does not have Value and Text properties in it. For example, If I bind a DataTable to a ListBox control, The ListBox.SelectedItem returns an object of System.Data.DataRowView.

    Only System.Web.ListBox control has the SelectedItem property which returns System.Web.UI.WebControls..ListItem object. The ListItem object has Value and Text properties in it.

    For WinForms ListBox, I think we can use ListBox.Text property to get the Text and ListBox.SelectedValue to get the value specified by the ValueMember property.

    Is my understanding correct?

    correct
    Friday, June 18, 2010 5:58 PM
  • string NameOfSelectedIndex = listBox1.SelectedItem.ToString();
    
    //in the NameOfSelectedIndex you retrive the text from selected index.

    Friday, June 18, 2010 6:54 PM
  • Hi Dukenathan,

    SelectedValue only returns something sensible if you set the "ValueMember" property...

    If you don't set that property (or can't because the object in the listbox doesn't have a property you want to use), you should use SelectedItem instead...

    So, make sure to set the DataSource of the listbox, instead of adding the items manually, eg:

    Code snippet:

            private void Form1_Load(object sender, EventArgs e)
            {
                List<string> values = new List<string>();
                values.Add("1111");
                values.Add("2222");
                listBox1.DataSource = values;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(listBox1.SelectedValue.ToString());
            }

    And now, the SelectedValue will return you the string representation.

    Best Wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, June 21, 2010 7:52 AM
    Monday, June 21, 2010 7:52 AM