Should I use a Winforms combobox's SelectedItem, SelectedText, or SelectedValue?
Asked Answered
W

5

12

I want to pass a value in a combo box as a param to a SQL statement. The Winforms combobox gives me several options for retrieving the value, namely SelectedItem, SelectedText, and SelectedValue. Which one is best/safest to use in this scenario?

Whiten answered 24/4, 2012 at 20:8 Comment(1)
What is the value for the DropDownStyle property?Nkvd
S
9

SelectedValue is probably the best one to use
SelectedText will give you the selected text of the editable portion, Selected Item will return you the object and selected index will return you the index. Usually for applications SelectedValue is extracted and used. Check out Combobox from MSDN

SelectedIndex   Gets or sets the index specifying the currently selected item.                (Overrides ListControl.SelectedIndex.)
SelectedItem    Gets or sets currently selected item in the ComboBox.
SelectedText    Gets or sets the text that is selected in the editable portion of a ComboBox.
SelectedValue   Gets or sets the value of the member property specified by the ValueMember property. (Inherited from ListControl.)
Spang answered 24/4, 2012 at 20:14 Comment(1)
I finally got around to testing this; when I tried SelectedValue, it bombed. SelectedItem works, though: //String Center = comboBoxCenters.SelectedValue.ToString(); <- err msg about object not being instantiated (and yes, it was null when I stepped through it) String Center = comboBoxCenters.SelectedItem.ToString(); // <- This works fineWhiten
N
9
if (comboBox1.DropDownStyle == DropDownStyle.DropDown || 
    comboBox1.DropDownStyle == DropDownStyle.Simple)
{
    return comboBox1.Text;
}

Text is probably the best one to use. This gets whatever is the currently selected text from the ComboBox as a string.

if (comboBox1.DropDownStyle == DropDownStyle.DropDownList)
{
    return comboBox1.GetItemText(comboBox1.SelectedItem);
}

For this style, you cannot get the text from the ComboBox. This returns the string from the item at the currently SelectedIndex instead.

Nkvd answered 24/4, 2012 at 20:12 Comment(0)
S
9

SelectedValue is probably the best one to use
SelectedText will give you the selected text of the editable portion, Selected Item will return you the object and selected index will return you the index. Usually for applications SelectedValue is extracted and used. Check out Combobox from MSDN

SelectedIndex   Gets or sets the index specifying the currently selected item.                (Overrides ListControl.SelectedIndex.)
SelectedItem    Gets or sets currently selected item in the ComboBox.
SelectedText    Gets or sets the text that is selected in the editable portion of a ComboBox.
SelectedValue   Gets or sets the value of the member property specified by the ValueMember property. (Inherited from ListControl.)
Spang answered 24/4, 2012 at 20:14 Comment(1)
I finally got around to testing this; when I tried SelectedValue, it bombed. SelectedItem works, though: //String Center = comboBoxCenters.SelectedValue.ToString(); <- err msg about object not being instantiated (and yes, it was null when I stepped through it) String Center = comboBoxCenters.SelectedItem.ToString(); // <- This works fineWhiten
C
9

It depends on 3 things 1. Mode 2. DropDownStyle 3. Required Value

On ComboBox.SelectedIndexChanged

  • Unbound Mode

    a. DropDownStyle = DropDown

    • SelectedItem will return = SelectedText
    • SelectedValue will return = ""
    • SelectedText will return = SelectedText

      b. DropDownStyle = DropDownList

      • SelectedItem will return = SelectedText
      • SelectedValue will return = ""
      • SelectedText will return = ""
  • Use Data Bound Mode (Means you are populating your ComboBox from some data source i.e SQL Server Table) You will select a Column of the table as DisplayMember and the same or another column as ValueMember.

    a. DropDownStyle = DropDown

    • SelectedItem will return = System.Data.DataRowView (Prompt)
    • SelectedValue will return = Value of ValuMemeber
    • SelectedText will return = SelectedText (Value of DisplayMember)

      b. DropDownStyle = DropDownList

      • .SelectedItem will return = System.Data.DataRowView (Prompt)
      • .SelectedValue will return = Value of ValueMember
      • .SelectedText will return = ""

Note: You can also use .Text that will return = Text of ComboBox

Conclusion:

  1. Unboud Mode

    • .SelectedItem is the best choice
  2. Data Bound Mode

    a. ValueMember is required

    • .SelectedValue is the best choice

      b. DisplayMember is required

      • .Text is the best choice
Cowl answered 9/8, 2017 at 8:49 Comment(1)
Thanks for explaining in detail. This is really helpful.Patrimony
F
2

Microsoft suggest using this for value

ComboBox1.SelectedItem.ToString()
Farias answered 9/11, 2022 at 16:37 Comment(0)
W
0

SelectedItem seems to be a safe choice.

I had this code:

NRBQConsts.currentSiteNum = listBoxSitesWithFetchedData.SelectedValue.ToString();

...which crashed with an NRE.

After changing it to this:

NRBQConsts.currentSiteNum = listBoxSitesWithFetchedData.SelectedItem.ToString();

...it works fine.

Whiten answered 30/1, 2015 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.