Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

ComboBox in C#


••
In Windows Forms, ComboBox provides two different features in a single control,
it means ComboBox works as both TextBox and ListBox. In ComboBox, only one
item is displayed at a time and the rest of the items are present in the drop-down
menu. The ComboBox is a class in C# and defined
under System.Windows.Forms Namespace. You can create ComboBox using the
two different ways:

1. Design-Time: It is the easiest method to create a ComboBox control using the


following steps:

Step 1: Create a windows form as shown in the below image:

• Visual Studio -> File -> New -> Project -> WindowsFormApp

• Step 2: Drag the ComboBox control from the ToolBox and drop it on the
windows form. You are allowed to place a ComboBox control anywhere on
the windows form according to your need.

• Step 3: After drag and drop you will go to the properties of the ComboBox
control to set the properties of the ComboBox according to your need.

Output:
ComboBox Items
The Items property is used to add and access items in a ComboBox. We can add
items to a ComboBox at design-time from Properties Window by clicking on Items
Collection as you can see in Figure 5.

Figure 5

When you click on the Collections, the String Collection Editor window will pop up
where you can type strings. Each line added to this collection will become a
ComboBox item. I add four items as you can see from Figure.
Code for Add Item on button Click
private void button1_Click(object sender, EventArgs e)
{

comboBox1.Items.Add(textBox1.Text);
MessageBox.Show(textBox1.Text + " added in the ComboBox");

ComboBox SelectedIndexChanged Event Hander


Code for Selecting Item from ComboBox on Selected
IndexChanged

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)


{
MessageBox.Show(comboBox1.Text);

MessageBox.Show(comboBox1.SelectedItem + " Selected in the ComboBox");

Run-Time: It is a little bit trickier than the above method. In this method, you can
set create your own ComboBox control using the ComboBox class. Steps to create a
dynamic ComboBox:
• Step 1: Create a combobox using the ComboBox() constructor is provided
by the ComboBox class.
// Creating combobox using ComboBox class
ComboBox mybox = 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 in 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);
• Example:
CSharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp18 {

public partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
// Creating and setting the properties of label
Label l = new Label();
l.Location = new Point(22, 50);
l.AutoSize = true;
l.Text = "Select Programming Language";

// Adding this label to the form


this.Controls.Add(l);

// Creating and setting the properties of comboBox


ComboBox mybox = new ComboBox();
mybox.Location = new Point(32, 80);
mybox.Size = new Size(216, 26);
mybox.Items.Add("C#");
mybox.Items.Add("Java");
mybox.Items.Add("Scala");
mybox.Items.Add("C");
mybox.Items.Add("C++");

// Adding this ComboBox to the form


this.Controls.Add(mybox);
}
}
}

• Output:
Properties of the ComboBox
Property Description
BackColor This property is used to set the background color 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 color 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 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.
Important 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 event 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 event occurs when the DropDownStyle property has


changed.
Leave This event occurs when the input focus leaves the ComboBox
control.

MouseClick This event occurs when the ComboBox control is clicked by the
mouse.

MouseDoubleClick This event 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.

You might also like