ComboBox Control

A ComboBox displays a combined text box with a ListBox that allows the user to select items from the list or enter a new value.

The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down. The property of DropDownStyle also specifies whether it is possible to edit the text portion. There is no setting to display the list at all times and to refuse to enter a new value. Use a ListBox control to display a list where no new values can be added.

We can add individual objects with the Add method. We can delete items with the Remove method or clear the entire list with the Clear method.

In addition to the display and selection functionality, the ComboBox also offers features that enable you to add items efficiently to the ComboBox and find text within the items of the list. With the BeginUpdate and EndUpdate methods, we can add a large number of items to the ComboBox without the control being repainted each time an item is added to the list. The FindString and FindStringExact methods enable you to search for an item in the list that contains a specific search string.

We can use these properties to manage the currently selected item in the list, the Text property to specify the string displayed in the editing field, the SelectedIndex property to get or set the current item, and the SelectedItem property to get or set a reference to the object.

Examples

The following code example is a complete application showing how we can use the Add method to add items to a ComboBox, the FindString method to find items in a ComboBox, and the BeginUpdate and EndUpdate methods to efficiently add a large number items to a ComboBox. The ability to store values that are different from displayed text is inherited from ListControl.

We must add references to the System.Drawing and System.Windows.Forms namespaces to run this example.

select combo box and goto property window, Now find Items property click on collection, then enter the strings in the collection (one string per line).

Combo Box Control - UK Academe

Or,

We can aslo write code on coding window on Button click.


            cobBox2.BeginUpdate();
            cobBox2.Items.Add("White");
            cobBox2.Items.Add("Red");
            cobBox2.Items.Add("Black");
            cobBox2.Items.Add("Green");
            cobBox2.EndUpdate();

How to retrieve value from ComboBox ?

If we want to retrieve the displayed item to a string variable , then we can code like this:


 string var;
var = comboBox1.Text;

Or


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

How to remove an item from ComboBox ?

We can remove items from a combobox in two ways. We can remove item at a the specified index or giving a specified item by name.


 comboBox1.Items.RemoveAt(1);

The code above will remove from the combobox the second item.


comboBox1.Items.Remove("White");

The code above will remove from the combobox the item "White".

The property of DropDownStyle specifies whether the list is always displayed or whether a drop - down displays the list. The property of DropDownStyle also specifies whether it is possible to edit the text portion.

ComboBox DropDownStyle-UK Academe

How to find string ComboBox?

This method's search is not case-sensitive. The s parameter is a substring that can be compared to the text in the combo box list associated with the items. The search performs a partial match from the beginning of the text and returns the first item in the list corresponding to the specified substring. We can then perform tasks, such as removing the item that contains the search text using the Remove method or changing the item's text. Once you have found the specified text, if we want to search for other instances of the text in the ComboBox, we must use the version of the FindString method that provides a parameter for specifying a starting index within the ComboBox. If we want to perform a search for an exact word match instead of a partial match, use the FindStringExact method.

Example:

int index = cobBox1.FindString("Black");
            cobBox1.SelectedIndex = index;

 

What is Selected Index Changed Event?

For the SelectedItemChanged event to occur, it is possible to change the SelectedItem property in code by typing the user in a new value or by clicking the up or down buttons of the

control. The following example of code shows the use of this member. In the example, the SelectedItemChanged event is reported by an event handler.

This report helps you to learn when the event occurs

Example:

If we select "Weekdays" from ComboBox1, then we got 3 weekdays on ComboBox2, and if we select "Year" from ComboBox1, then we got 3 year on ComboBox2.


private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add("weekdays");
            comboBox1.Items.Add("year");
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            if (comboBox1.SelectedItem == "weekdays")
            {
                comboBox2.Items.Add("Sunday");
                comboBox2.Items.Add("Monday");
                comboBox2.Items.Add("Tuesday");
            }
            else if (comboBox1.SelectedItem == "year")
            {
                comboBox2.Items.Add("2012");
                comboBox2.Items.Add("2013");
                comboBox2.Items.Add("2014");
            }
        }

Output:

If we select "Weekdays"

ComboBox Example1- UK_Academe

If we select "Year"

ComboBox Example2-UK_Academe

How to make a combobox read only?

We can make a ComboBox readonly, that means a user cannot write in a combo box but he can select the given items, in two ways.

By default, DropDownStyle property of a Combobox is DropDown. In this case user can enter values to combobox. When you change the DropDownStyle property to DropDownList, the

Combobox will become read only and user can not enter values to combobox ( comboBox1.DropDownStyle = ComboBoxStyle.DropDown ) .

Second method, if you want the combobox completely read only, you can set comboBox1.Enabled = false.

ComboBox Source Code

 

Video Tutorial