Open In App

ComboBox in C#

Last Updated : 25 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Windows Forms, the ComboBox control combines the features of a TextBox and a ListBox. It displays one item at a time, with additional items accessible through a drop-down menu. The ComboBox class is part of the System.Windows.Forms namespace.

Ways to Create a ComboBox In Windows Forms

There are mainly two ways to create a ComboBox in Windows forms which are mentioned below.

  • Drag and drop (Design-Time)
  • Custom ComboBox (Run-Time)

Drag and drop (Design-Time)

This is the easiest way to create a ComboBox in Windows Forms using Visual Studio we just have to open the toolbox and drag and drop the button on the form in the designer and further we can change the appearance of the ComboBox using the properties. Follow these steps to create a ComboBox.

Step 1: Now locate the project with the name here we are using the default name which is Form1 and it will open a form in the editor that we can further modify.

Empth-forms

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form1.cs file for custom logic.


Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

ToolBox



Step 3. Now open the common controls and drag and drop the ComboBox on the form where we want to it be placed.

ComboBoxDragandDrop


Step 4. Now open the properties of the button press right-click on the ComboBox and it will open the properties solution explorer now we can change the appearance and the behaviour of the button.

Properties


Step 5: In the properties, we can make different types of changes we can add the items in the list which is available at the left-most bottom in the properties as shown in the image

EditItems


It will open the new box (String Collection Editor ) here we can add a different list and each line is a separate list as shown in the image after adding the list click on the OK button.

EditItemsList


Output:

file


Custom ComboBox (Run Time)

In this method, we are going to modify the Form1.cs file and add custom code modification in C# with the help of the ComboBox class. The following steps show how to create a ComboBox dynamically:

Step 1: Create a ComboBox using the ComboBox() constructor provided by the ComboBox class.

// Creating ComboBox

ComboBox MyComboBox = new ComboBox();


Step 2: After creating ComboBox, set the properties of the ComboBox provided by the ComboBox class.

// Set the location of the ComboBox

mybox.Location = new Point(327, 77);


// Set the size of the ComboBox

mybox.Size = new Size(216, 26);


// Add items to the ComboBox

mybox.Items.Add("C#");

mybox.Items.Add("Java");

mybox.Items.Add("Scala");

mybox.Items.Add("C");

mybox.Items.Add("C++");


Step 3: And last add this ComboBox control to form using Add() method.

// Add this ComboBox to the form

this.Controls.Add(mybox);


Step 4: Now double-click on the form in Design and it will open the Form1.cs file where code is written in C#. Here the program file is Form 1.cs Now write this code in Form1.cs file

Form1.cs file:

C#
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the properties of the label
            Label l = new Label
            {
                Location = new Point(122, 80),
                AutoSize = true,
                Text = "Select Programming Language"
            };

            // Creating and setting the properties of the ComboBox
            ComboBox mybox = new ComboBox
            {
                Location = new Point(327, 77),
                Size = new Size(216, 26)
            };

            // Adding items to the ComboBox
            mybox.Items.AddRange(new object[] { "C#", "Java", "Scala", "C", "C++" });

            // Adding controls to the form
            this.Controls.Add(l);
            this.Controls.Add(mybox);

        }
    }
}


Output:

file


Properties

PropertyDescription
BackColorThis property is used to set the background colour for the ComboBox control.
DropDownHeightThis property is used to set the height in pixels of the drop-down portion of the ComboBox control.
DropDownStyleThis property is used to set a value specifying the style of the ComboBox control.
DropDownWidthThis property is used to set the width of the drop-down portion of a ComboBox control.
FontThis property is used to set the font of the text displayed by the ComboBox control.
ForeColorThis property is used to set the foreground colour of the ComboBox control.
HeightThis property is used to set the height of the ComboBox control.
ItemsThis property is used to get an object representing the collection of the items contained in this ComboBox control.
MaxDropDownItemsThis property is used to set the maximum number of items to be shown in the drop-down portion of the ComboBox control.
MaxLengthThis property is used to set the number of characters a user can type into the ComboBox control.
NameThis property is used to set the name of the ComboBox control.
SelectedItemThis property is used to set the currently selected item in the ComboBox.
SizeThis property is used to set the height and width of the ComboBox control.
SortedThis property is used to set a value indicating whether the items in the combo box are sorted.
TextThis property is used to set the text associated with this ComboBox control.
VisibleThis property is used to set a value indicating whether the control and all its child controls are displayed.


Events

EventDescription
ClickThis event occurs when the ComboBox control is clicked.
DragDropThis event occurs when a drag-and-drop operation is completed.
DropDownThis even occurs when the drop-down portion of a ComboBox is shown.
DropDownClosedThis event occurs when the drop-down portion of the ComboBox is no longer visible.
DropDownStyleChangedThis occurs when the DropDownStyle property has changed.
LeaveThis occurs when the input focus leaves the ComboBox control.
MouseClickThis occurs when the ComboBox control is clicked by the mouse.
MouseDoubleClickThis occurs when the ComboBox control is double-clicked by the mouse.
MouseDownThis event occurs when the mouse pointer is over the ComboBox control and a mouse button is pressed.
MouseEnterThis event occurs when the mouse pointer enters the ComboBox control.
MouseHoverThis event occurs when the mouse pointer rests on the ComboBox control.
SelectedIndexChangedThis event occurs when the SelectedIndex property has changed.

Next Article

Similar Reads