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

18,677

Solution 1

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

Solution 2

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.

Solution 3

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
Share:
18,677
B. Clay Shannon-B. Crow Raven
Author by

B. Clay Shannon-B. Crow Raven

My novel about climate change and social justice featuring talking animals traveling through time and space to prevent disasters is now available on amazon, in three formats: Taterskin & The Eco Defenders Kindle eBook; Taterskin & The Eco Defenders Paperback; Taterskin & The Eco Defenders Hardcover Taterskin & The Eco Defenders, told in “first canine” by the titular character, a Labrador Retriever, is the story of a few humans and several talking animals who travel through time and space to make the past—and thus the future—a better place. The improvements effected by the Eco Defenders benefit not just the earth itself, but also mistreated humans and animals. In Book 1 (“Wonders Never Cease”), The Eco Defenders travel 150 million years into the past, to meet a Pterodactyl and make plans to “nip Nazism in the bud.” After that, it's on to 1787 Australia to protect the indigenous people and the environment there. The Eco Defenders next go to India, where they assemble animals from all over that country to put an end to Thuggee and fights to the death between Cobras and Mongooses. Their final stop is 1885 Africa, where the Eco Defenders band together with the local animals to prevent King Leopold of Belgium from taking control of the Congo, following which they put an end to the poaching of animals throughout the continent. Book 2 (“Tell it to Future Generations”) takes up with the Eco Defenders following up on their earlier adventures by 1) Preventing the American Civil War in 1861, after which a slave they free joins them; 2) Saving the Indians from being massacred at Wounded Knee in 1890, following which Chapawee, a Sioux Indian, joins the Eco Defenders; 3) Putting an end to the practice of vivisection (experimentation on live animals) in 1903; 4) Coming to the aid of exploited workers in 1911 Manhattan, saving hundreds from the Triangle Shirtwaist Fire; and 5) Traveling to the Amazon Basin in 1978 to protect and preserve the Amazon rainforest. @@@@@@@@@@@@@@@@@@@@@@@ I have lived in eight states; besides my native California (where I was born and where I now again reside), in chronological order I have infested: New York (Brooklyn), Montana (Helena), Alaska (Anchorage), Oklahoma (Bethany), Wisconsin (New Berlin and Oconomowoc), Idaho (Coeur d'Alene), and Missouri (Piedmont). I am a writer of both fiction (for which I use the nom de guerre "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006 and can be downloaded gratis here.

Updated on June 13, 2022

Comments

  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven almost 2 years

    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?

  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven almost 12 years
    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 fine
  • Rahul Techie
    Rahul Techie over 3 years
    Thanks for explaining in detail. This is really helpful.