How to Add a ComboBox to a C# Windows Form

Adding and Populating a C# ComboBox in a Windows Application

© Mark Alexander Bain

Apr 16, 2009
Working with a C# ComboBox, Mark Alexander Bain
One Reason for using a Windows form based application is to give the user many choices. One very effective way to to give the user choices is to create a C# ComboBox.

One of the most useful elements on any Windows form is the ComboBox (often known as a drop-down box). It allows the user to choose from an item from defined list of options (for instance the days of the week) and then the programmer can use the choice elsewhere in the application (for example to tell the user from which planet the day name is derived). All of this can be achieved very easily with C#.

Defining a ComboBox with C#

The C# programmer creates a ComboBox by using C#'s ComboBox object, and initializes it as part of their form's definition:

public class MainForm : Form {
private ComboBox cboExample = new ComboBox();

Having create the ComboBox, the programmer's next job is to populate it.

Using C# to populate a ComboBox.

The most effective way to populate a ComboBox is to create a function to do the job:

public static void Main() {
Application.Run(new MainForm());
}
public MainForm() {
setUpComboBox();
}

The programmers can then add the individual elements that make up the ComboBox:

public void setUpComboBox () {
cboExample.Items.Add("Sunday");
cboExample.Items.Add("Monday");

Or by adding a group of items in the form of an array:

cboExample.Items.AddRange(strContents);

Of course, before that can be done the programmer must define a suitable array:

private string[] strContents = {
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};

And the next task that the function must carry out is to define where the ComboBox is to be located on the form, and then to add it as a component of the form (the ComboBox will not be visible unless this is done):

cboExample.Location = new Point (50,0);
this.Controls.Add(cboExample);

If the application is now compiled then the user will see a ComboBox containing the days of the week. The user will be able to change the selected item (which may be used elsewhere in the application), but nothing else will happen at this point. However, the programmer can add functionality to the ComboBox by adding a C# event handler.

Adding Functionality to a C# ComboBox

By default the user will be able to select items in the ComboBox but that is all. The programmer can write additional functions that use the contents of the ComboBox, but they can also create a function that will run whenever the ComboBox selection is changed. The programmer does this by adding an event listener:

cboExample.SelectedIndexChanged +=
new EventHandler(this.combo_selected);

Now the function "combo_selected" will run whenever the user changes the selected item. This means that the programmer will need to write the function to be called by the event handler:

public void combo_selected(object Sender, EventArgs e) {
MessageBox.Show(
"Day = " + cboExample.Text
+ ": Planet = " + planets[cboExample.SelectedIndex]);
}

In this example the function returns a message box containing day selected and the planet from which the day name is derived. It does this by using the selected item's index number to reference the contents of an array such as:

private string[] planets = {
"Sun",
"Moon",
"Mars",
"Mercury",
"Jupiter",
"Venus",
"Saturn"};

If the form is compiled and the user selects "Friday" (for example) then they will be informed that the day's name originates from the Planet Venus.

Summary

A programmer uses C#'s ComboBox object to add a ComboBox to their own form. The process is:

  • Initialize the ComboBox object
  • Add items individually or as an array
  • Define the location for the ComboBox on the form
  • Add the ComboBox to the form as a control (so that it becomes visible)
  • Finally add an event handler (if required)

And in just those few steps the programmer can easily add a very useful ComboBox to any of their forms.


The copyright of the article How to Add a ComboBox to a C# Windows Form in C Programming is owned by Mark Alexander Bain. Permission to republish How to Add a ComboBox to a C# Windows Form in print or online must be granted by the author in writing.


Working with a C# ComboBox, Mark Alexander Bain
A C# ComboBox, Mark Alexander Bain
Selecting an Item from a C# ComboBox, Mark Alexander Bain
A C# ComboBox Event Handler, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Post Your Comment
NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
What is 5+0? Incorrect, please resolve x + y!
Comments
May 26, 2009 9:53 PM
Guest :
the info is really very good ! thanks :)
1 Comment: