Windows Forms: ComboBox in C#

Add item or binding item to combobox and get selected item from combobox in C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "ComboboxDemo" and then click OK

combobox c#Step 2: Design your form as below

c# combobox

Step 3: Create a State class to map data

public class States
{
    public string ID { get; set; }
    public string Name { get; set; }
}

Step 4: Add code to handle your form as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void cboState_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblValue.Text = cboState.Text;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Add item to combobox
            cboState.Items.Add("Arizona");
            cboState.Items.Add("Ohio");
            cboState.SelectedIndex = 1;
            //Clear all item cboState2
            cboState2.Items.Clear();
            //Init data
            List<States> list = new List<States>();
            list.Add(new States() { ID = "01", Name = "Texas" });
            list.Add(new States() { ID = "02", Name = "Ohio" });
            list.Add(new States() { ID = "02", Name = "Utah" });
            list.Add(new States() { ID = "04", Name = "Vermont" });
            //Set display member and value member for combobox
            cboState2.DataSource = list;
            cboState2.ValueMember = "ID";
            cboState2.DisplayMember = "Name";
        }

        private void cboState2_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //Cast item to State object
            States obj = cboState2.SelectedItem as States;
            if (obj != null)
                lblValue.Text = obj.Name;
        }
    }
}

VIDEO TUTORIALS