CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C# – How to use ComboBox control?

ComboBox control is one of the most useful control in Windows Forms. It provides an interface to display the list of items, and allows to select the items. To save the space on Windows Form, it lists the items as a drop-down list. That means, initially the control displays a text box & a button. When click on the button, it displays the list of items and the selected item will be displayed in a text box.

ComboBox control is a combination of text box, button & list box controls.

Properties

We need to add the entries to ComboBox control, in order to display them when needed. As mentioned above, ComboBox control displays it’s items in a list box. It holds the list of items in it’s Items collection, which is an important property of this control. That said, Items property is of collection type. To add or remove the items to & from the ComboBox control (see below Methods section), we use this Items property.

When Sorted property is set to True , it’s items will be sorted.

Text property returns the text displayed in the text box. That means, it returns the selected item’s text. This is editable, but it will be disabled depends on the value set in DropDownStyle property.

DropDownStyle property controls the appearance and functionality of the ComboBox control. Depending on your program requirement, you can set this property to proper value. When set this property to;

  • Simple, it will display it’s items in the list box and a text box will be displayed to display the selected item from it’s list box. Text box is allowed to edit the text it displays.
  • DropDown, it displays a text box and a button control next to it. When click on the button control, it shows the list of items the control has and allows to select the item(s). The selected item will be displayed in the text box and it is editable.
  • DropDownList, it behaves like when we set this property to DropDown; except, the text box is NOT editable. That means, when we select the item from the ComboxBox control, it displays the selected item in the text box and but it doesn’t allow to edit the text. When set this; Text property will behave like read-only.

Another useful property is, MaxDropDownItems property. This allows to set the number of items to be visible at a time, when drop-down list is displayed. This you set to the proper value, depending on your application window size.

Items in ComboBox control has the index values. When we select an item from the control, the SelectedIndex value will be updated with the selected item’s index value. This is very useful property to identify which item is selected in the ComboBox control.

Methods

Adding and removing items to & from the ComboBox control can be done through the methods of Items property. Items.Add method will add an item to the ComboBox control. And, Items.Remove method will be used to remove an item from the ComboBox control. To remove the item from the specific position index, we can use Items.RemoveAt method.

To remove all items from the ComboBox control, we can use Items.Clear() method.

Events

SelectedIndexChanged event will be triggered whenever we select an item from the ComboBox control. Actually this will be triggered whenever SelectedIndex property value is changed.

DropDown event will be raised when the drop-down portion of the ComboBox control is visible. Usually this will be visible when we click on the button to select an item from the ComboBox control. Once the selection is done, the drop-down portion will be hidden. Then, DropDownClosed event will be triggered. These two events are very useful to place the code to update or reset the changes for the ComboBox control.

Working Example

It’s time to put all together to create a working example. Here is the complete code;

Screenshot

Upon successful execution of the program, the Application window looks like below;

ComboBox control demo
ComboBox control demo

(Raju)

C# – How to use ComboBox control?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top
Exit mobile version