Table of contents
1.
Introduction
1.1.
Design-Time
1.2.
Run-Time
1.3.
Properties of ComboBox
1.4.
Important Events
2.
Frequently Asked Questions
2.1.
What is ComboBox control?
2.2.
What is the default event of ComboBox control?
2.3.
What is the difference between ComboBox and drop-down list content control?
2.4.
What is the difference between a list box and a combo box?
2.5.
How do you use combo boxes in Visual Basic?
3.
Conclusion
Last Updated: Mar 27, 2024

C# ComboBox Control

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

ComboBox is also an essential feature of Windows Forms. ComboBox combines two functions into a single control, allowing it to function as TextBox and a ListBox.  Although, only one item can be presented at a particular time. The remaining items are then stored in a drop-down menu. 

The class name is ComboBox, and its definition is present under the namespace System.Windows.Forms.

In C#, ComboBox can be created by two different methods:

  1. Design-Time
  2. Run-Time

Let us see these method in details with their implementation.

Design-Time

This one is the easiest way to create a ComboBox. 

Following are the steps to do it.

  • The first step will be to create a form window, as shown in the below image.
  • In Visual Studio, you will have to click on the File menu, then go to New. After that, click on Project, then WindowsFormApp.

  • In the second step, we will drag and drop the ComboBox Control available in the toolbox to the form. 

 

The ComboBox can be placed anywhere on the form as per requirements.

  • After successful drag and drop, the properties of the ComboBox control can be modified easily as per your requirements. 

And, here we are done with the first method, let us now look at the second one: 

Run-Time

In this method, you can create a ComboBox control using a program with the help of syntax provided by the ComboBox class.

Following are the steps to create a ComboBox control dynamically.

The ComboBox class provides the ComboBox() constructor for creating a ComboBox control.

  • The first step is to create a textbox with the help of the ComboBox() constructor.

 

/* 
     Creating ComboBox with the help of ComboBox class
*/

ComboBox comboBox1 = new ComboBox();

 

  • After we have successfully created the ComboBox control, we have to set the property as per our requirement of the ComboBox control provided by the ComboBox class in the second step.

 

// Set the location of the ComboBox 
comboBox1.Location = new Point(327, 77);


// Set the size of the ComboBox
comboBox1.Size = new Size(216, 26);


// Add items in the ComboBox
comboBox1.Items.Add("Mahesh Chand");  
comboBox1.Items.Add("Mike Gold");  
comboBox1.Items.Add("Praveen Kumar");  
comboBox1.Items.Add("Raj Beniwal"); 

 

  • The last step is to add the created RichTextBox control to the form. This can be done by:

 

// Add this ComboBox to the form
this.Controls.Add( comboBox1);

 

Now, it’s time for an example:

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Threading.Tasks;
 
namespace WindowsFormsApp18 {
  
public partial class Form1 : Form {
  
    public Form1()
    {
        InitializeComponent();
    }
  
    private void Form1_Load(object sender, EventArgs e)
    {
        // Creating comboBox and setting its properties
        ComboBox  comboBox1 = new ComboBox();
         comboBox1.Location = new Point(327, 77);
         comboBox1.Size = new Size(216, 26);
        comboBox1.Items.Add("Mahesh Chand");  
        comboBox1.Items.Add("Mike Gold");  
        comboBox1.Items.Add("Praveen Kumar");  
        comboBox1.Items.Add("Raj Beniwal"); 


        // Adding this ComboBox to the form
        this.Controls.Add( comboBox1);
    }
}
}

 

Output:

Properties of ComboBox

Some of the essential properties of ComboBox are:

  • BackColor: This property helps to set the background color of the ComboBox control.
  • DropDownWidth: This property is used to specify the width of the drop-down portion of the ComboBox.
  • DropDownHeight: This property helps to set the height of the drop-down portion of the ComboBox control in pixels.
  • DropDownStyle: This property is used to specify the style of the ComboBox using a value.
  • ForeColor: This property helps to set the foreground color of the ComboBox control.
  • Items: This property helps in getting an object with a collection of the items present in the ComboBox.
  • MaxLength: This property allows us to set the length of characters that can be typed in the ComboBox.

Important Events

Some of the important events are:

  • Click: This event happens when the ComboBox control is clicked.
  • DropDown: This event happens when the drop-down portion of the ComboBox is visible.
  • DragDrop: This event happens when a successful drag and drop operation is completed.
  • Leave: This event happens when any input focus moves away from the ComboBox control.
  • DropDownClosed: This event happens when the drop-down portion of the ComboBox is not visible.

Frequently Asked Questions

What is ComboBox control?

The Windows Forms ComboBox control displays data in a drop-down combo box. By default, the ComboBox control appears in two parts: the top part is a text box that allows the user to type a list item. The second part is a list box that displays a list of items from which the user can select one.

What is the default event of ComboBox control?

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.

What is the difference between ComboBox and drop-down list content control?

A drop-down list can be said to as a list where the visibilty of the selected item is always authentic, and the others are visible on demand by clicking a drop-down button. A combo box combines a drop-down list or a standard list box and a text box that is editable, thus allowing users to enter a value that isn't in the list.

What is the difference between a list box and a combo box?

The List box and the Combo box are both used in visual basics to display a list of items. The List box shows all the items at once in a text area, whereas the combo box displays only one item at a time. The rest of the items will be in a drop-down list for a combo box which can be viewed after clicking on it.

How do you use combo boxes in Visual Basic?

First, double-click the icon to add a Combo Box to your form. Or click once with the left-hand mouse button. After that, draw one on the windows form. A combo box is one of the ways to limit your user's choices. A drop-down list of items appears when a black down-pointing arrow is clicked.

Also Read About - singleton design pattern in c# and Ienumerable vs Iqueryable

Conclusion

In this article, we have studied about ComboBox in C#. We have also discussed the properties of ComboBox and different events in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding ComboBox in C# with different ways to create it and if you would like to learn more, check out our articles on C# GenericsC# Variables and C# Reflection.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available, Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass