How to Get Selected Value of a ComboBox in C#

  1. Use the ComboBox.SelectedItem Property to Get the Selected Value of a ComboBox in C#
  2. Use ComboBox.GetItemText to Get Selected Value of a Combobox in C#
How to Get Selected Value of a ComboBox in C#

In this tutorial, you will learn the different methods to get the selected text and values of a ComboBox in C#. The most common method of getting a ComboBox control’s selected value is fetching it in a button click event using C#.

A ComboBox control in C# provides a combined functionality of a text box and a list box in a single control. The two primary methods to display and get the selected value of a ComboBox are using Combobox.SelectedItem and ComboBox.GetItemText properties in C#.

A selected item’s value can be retrieved using the SelectedValue property. You can create a ComboBox control using a Forms designer at design-time or using the ComboBox class in C# code at run-time.

Use the ComboBox.SelectedItem Property to Get the Selected Value of a ComboBox in C#

In .NET’s ComboBox control, the .SelectedItem property displays a string representation of the selected value. The ComboBox.SelectedItem property of a ComboBox in C# can get or set the currently selected item in a ComboBox.

The selected value of a ComboBox is null initially and only be assigned after the user sets the SelectedItem property to an object. When a user clicks on a value of a ComboBox, the value becomes the currently selected object/value in the list.

// its C# code of `Form1.cs` of `ComboboxSelectedvalue` project

using System;
using System.Windows.Forms;

namespace ComboboxSelectedvalue {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    // create a `comboBox1` ComboBox and `button1` button
    // use the `button1` to get the selected value of the `comboBox1`

    // it is a `button1` click event
    private void button1_Click(object sender, EventArgs e) {
      object b = comboBox1.SelectedItem;
      string be = Convert.ToString(b);
      MessageBox.Show("Your selected value is:" + be);
    }
  }
}

Use ComboBox.GetItemText to Get Selected Value of a Combobox in C#

The this.comboBox1.GetItemText(value) property of a ComboBox helps retrieve the displayed or selected value to a string variable. It’s extremely crucial for the selected item; however, there are times when it’s useful for the other values of a ComboBox.

The GetItemText method of getting the selected value of a combo box is consistent with the definition of the existing SelectedValue property and its implementation. It’ll return null when the provided object doesn’t belong in the control’s list, and it’ll return the value itself if it’s valid and the ValueMember is not set.

If you are unfamiliar, you can add a new class to your WinForms project, add the following code, and then build your project to have a new control ready to be dragged onto a form.

// its C# code of `Form1.cs` of `ComboboxSelectedvalue` project

using System;
using System.Windows.Forms;

namespace ComboboxSelectedvalue {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) {
      object b = comboBox1.SelectedItem;
      object be = comboBox1.GetItemText(b);

      MessageBox.Show("The value of your selected item is:" + be);
    }
  }
}

Create an instance of the ComboBox’s class, set its properties, and add a ComboBox instance to the Form controls to create a ComboBox control at run-time. Either you can set control properties of a ComboBox at design time or from the Properties Window of the Visual Studio IDE.

The value of ComboBox.SelectedText is empty at the start because the SelectedText property gets and sets the selected text in a ComboBox only when it has focused on it. If the focus moves away, its value will be an empty string.

In this article, you have learned to get the selected value of a ComboBox in C# at design-time and run-time. After that, you have discovered various properties and methods of ComboBox control in Windows Forms to get selected values.

Syed Hassan Sabeeh Kazmi avatar Syed Hassan Sabeeh Kazmi avatar

Hassan is a Software Engineer with a well-developed set of programming skills. He uses his knowledge and writing capabilities to produce interesting-to-read technical articles.

GitHub