HOWTO · Csharp
How to Get Selected Value of a ComboBox in C#
Learn how to read the selected item, displayed text, or bound value from a Windows Forms ComboBox in C#.
On this page
In a Windows Forms ComboBox, “selected value” can mean three different things: the selected item object, the text shown for that item, or its data-bound value. Read ComboBox.SelectedItem when you need the object, use ComboBox.GetItemText when you need its displayed text, and read SelectedValue when the control is bound and you need the field configured by ValueMember. These examples assume the control is already on a form and show the selection being read from a button click handler.
The examples below target a Windows Forms application. Runtime execution evidence was not supplied for this article, so they are presented as unexecuted Windows-specific examples; no console output is claimed.
Use the ComboBox.SelectedItem Property to Get the Selected Value of a ComboBox in C#
ComboBox.SelectedItem is a property that gets or sets the currently selected object in the list. It does not always return a string: when the items are custom objects, the result is that object. Convert it to text only when the item’s string representation is what the application needs. The following complete example creates a form, selects an item, and reads the current object when the button is clicked.
using System;
using System.Windows.Forms;
internal static class Program
{
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form();
var comboBox = new ComboBox { Left = 12, Top = 12, Width = 180 };
comboBox.Items.AddRange(new object[] { "Red", "Green", "Blue" });
comboBox.SelectedItem = "Green";
var button = new Button { Left = 12, Top = 48, Text = "Read selection" };
button.Click += (sender, args) =>
{
object selectedItem = comboBox.SelectedItem;
string selectedText = Convert.ToString(selectedItem) ?? "";
MessageBox.Show($"Item: {selectedText}");
};
form.Controls.Add(comboBox);
form.Controls.Add(button);
Application.Run(form);
}
}
In an existing form, the same operation can live in its click handler: read comboBox1.SelectedItem, then handle it according to the item’s actual type. If the list contains strings, the selected item is a string. If it contains objects, cast or pattern-match the returned object before using its properties. A selection can also be absent, so code that runs before an item is selected should account for SelectedItem being null.
Use ComboBox.GetItemText to Get Selected Value of a Combobox in C#
ComboBox.GetItemText(item) is a method that returns the text the control uses to display the supplied item. Call it with SelectedItem to obtain the current selection’s displayed text. This is useful when items are objects and the ComboBox uses DisplayMember; calling ToString() on the object may produce different text. GetItemText answers a display question, while SelectedValue answers a data-binding question.
For a bound ComboBox, set DisplayMember to the property shown to the user and ValueMember to the property that identifies the item. Then retrieve comboBox1.SelectedValue when the application needs that bound value. The example shows both results so you can choose the one your code needs:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
internal sealed class City
{
public int Id { get; set; }
public string Name { get; set; } = "";
}
internal static class Program
{
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
var form = new Form();
var comboBox = new ComboBox { Left = 12, Top = 12, Width = 180 };
comboBox.DisplayMember = nameof(City.Name);
comboBox.ValueMember = nameof(City.Id);
comboBox.DataSource = new List<City>
{
new City { Id = 1, Name = "Paris" },
new City { Id = 2, Name = "Tokyo" }
};
comboBox.SelectedValue = 2;
var button = new Button { Left = 12, Top = 48, Text = "Read selection" };
button.Click += (sender, args) =>
{
string displayedText = comboBox.GetItemText(comboBox.SelectedItem);
object selectedValue = comboBox.SelectedValue;
MessageBox.Show($"Text: {displayedText}; value: {selectedValue}");
};
form.Controls.Add(comboBox);
form.Controls.Add(button);
Application.Run(form);
}
}
Here GetItemText reads the display text (for example, Tokyo), while SelectedValue reads the bound Id (for example, 2). Use the value member configured on the control rather than treating display text as an identifier. Both examples require a Windows Forms project and were not executed in the supplied runtime environment.