How to Create Combobox in C#

How to Create Combobox in C#. In this article, we will look at the example to create Combobox in C#.

What is Combobox?

ComboBox is an ASP.NET AJAX control that combines the flexibility of a TextBox with a list of options from which users can choose. In simple words ‘This useful control is a combination of TextBox with a drop-down. Its drop-down list presents preset choices. The user can type anything into the ComboBox, or select something from the list.’

C# controls are located in the Toolbox of the development environment, and you use them to create objects on a form with a simple series of mouse clicks and dragging motions.

The ComboBox works like a combination between a standard ASP.NET DropDownList control and a TextBox control. You can either select from a pre-existing list of items or enter a new item.

The user can type a value in the text field or click the button to display a drop-down list. You can add individual objects with the Add method. You can delete items with the Remove method or clear the entire list with the Clear method.

How to add an item to Combobox?

comboBox1.Items.Add("Coffee");
comboBox1.Items.Add("Tea");
comboBox1.Items.Add("Orange Juice");
comboBox1.Items.Add("Milk");
comboBox1.Items.Add("Iced Tea");
comboBox1.Items.Add("Mango Shake");

How to retrieve value from ComboBox?

If you want to retrieve the displayed item to a string variable, you can use the following code

string var;
var = comboBox1.Text;
   Or
var item = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
MessageBox.Show(item);

How to remove an item from ComboBox?

You can remove items from a Combobox in two ways. You can remove an item at a specified index or giving a specified item by name.

comboBox1.Items.RemoveAt(3);

The above code will remove the fourth item from the Combobox which is “Milk” now you are thinking that RemoveAt(3) how can it remove the fourth item because the index starts from the 0th position so the 3rd index holds the fourth item in the list.

comboBox1.Items.Remove("Tea");

The above code will remove the item “Tea” from the Combobox.

DropDownStyle Property

The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down. The DropDownStyle property also specifies whether the text portion can be edited.

comboBox1.DropDownStyle = ComboBoxStyle.DropDown;

How to set a default value for a Combo Box?

You can set the Combobox default value by using the SelectedIndex property. The following code will set the 6th item as Combobox default value.

comboBox1.SelectedIndex = 6;

How to make a Combobox read-only?

You can make a ComboBox read-only, which means a user cannot write in a combo box but he can select the given items, in two ways. By default, the DropDownStyle property of a Combobox is DropDown. In this case, the user can enter values to Combobox. When you change the DropDownStyle property to DropDownList, the Combobox will become read-only and the user can not enter values to Combobox. The second method, if you want the Combobox completely read-only, you can set comboBox1.Enabled = false.

C# code for using Combobox

using System;
using System.Drawing;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("coffee");
            comboBox1.Items.Add("Tea");
            comboBox1.Items.Add("Orange Juice");
            comboBox1.Items.Add("Milk");
            comboBox1.Items.Add("Iced Tea");
            comboBox1.Items.Add("Mango Shake");
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string var;
            var = comboBox1.Text;
            MessageBox.Show(var);
        }
    }
}

In this way, we learn How to create a Combobox in C#.