A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/combobox-in-c-sharp/ below:

ComboBox in C# - GeeksforGeeks

ComboBox in C#

Last Updated : 12 Jul, 2025

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)

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.

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.

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

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.

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

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.

Output:

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:

Properties Property Description BackColor This property is used to set the background colour for the ComboBox control. DropDownHeight This property is used to set the height in pixels of the drop-down portion of the ComboBox control. DropDownStyle This property is used to set a value specifying the style of the ComboBox control. DropDownWidth This property is used to set the width of the drop-down portion of a ComboBox control. Font This property is used to set the font of the text displayed by the ComboBox control. ForeColor This property is used to set the foreground colour of the ComboBox control. Height This property is used to set the height of the ComboBox control. Items This property is used to get an object representing the collection of the items contained in this ComboBox control. MaxDropDownItems This property is used to set the maximum number of items to be shown in the drop-down portion of the ComboBox control. MaxLength This property is used to set the number of characters a user can type into the ComboBox control. Name This property is used to set the name of the ComboBox control. SelectedItem This property is used to set the currently selected item in the ComboBox. Size This property is used to set the height and width of the ComboBox control. Sorted This property is used to set a value indicating whether the items in the combo box are sorted. Text This property is used to set the text associated with this ComboBox control. Visible This property is used to set a value indicating whether the control and all its child controls are displayed.
Events Event Description Click This event occurs when the ComboBox control is clicked. DragDrop This event occurs when a drag-and-drop operation is completed. DropDown This even occurs when the drop-down portion of a ComboBox is shown. DropDownClosed This event occurs when the drop-down portion of the ComboBox is no longer visible. DropDownStyleChanged This occurs when the DropDownStyle property has changed. Leave This occurs when the input focus leaves the ComboBox control. MouseClick This occurs when the ComboBox control is clicked by the mouse. MouseDoubleClick This occurs when the ComboBox control is double-clicked by the mouse. MouseDown This event occurs when the mouse pointer is over the ComboBox control and a mouse button is pressed. MouseEnter This event occurs when the mouse pointer enters the ComboBox control. MouseHover This event occurs when the mouse pointer rests on the ComboBox control. SelectedIndexChanged This event occurs when the SelectedIndex property has changed.

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4