Home

Windows Control: The Combo Box

 

Fundamentals of Combo Boxes

 

Introduction

A combo box is a list of items that the user can select from. In the following screenshot, the Font Effects property page of the Character dialog box of OpenOffice.org presents the Underlining, the Color, the Effects, the Strikethrough, the Relief, and the Font Color combo boxes:

The Font Effects tab of the Character dialog box displays various combo boxes

Because a combo box does not (permanently) display its list, to show its content, the user can click the arrow button. Here is an example:

Creating a Combo Box

To support combo boxes, the .NET Framework provides a class named ComboBox. The ComboBox class is derived from the ListControl class. To create a combo box, declare a variable of type ComboBox, allocate its memory with the new operator and add it to the Controls collection of its container. Here is an example:

open System;
open System.Drawing;
open System.Windows.Forms;

let exercise : System.Windows.Forms.Form = new Form();

let cbxAcademicDisciplines : ComboBox = new ComboBox()

exercise.Controls.Add(cbxAcademicDisciplines)

cbxAcademicDisciplines.Location <- new Point(12, 32);

let lblTitle : Label = new Label();
lblTitle.Text <- "Academic Disciplines";
lblTitle.Location <- new Point(12, 12);
lblTitle.AutoSize <- true;
exercise.Controls.Add(lblTitle);

do
    System.Windows.Forms.Application.Run(exercise);

This would produce:

Combo Box

Characteristics of a Combo Box

 

Introduction

Like all visual controls, a combo box shares all the basic characteristics of other graphic controls: the location, the size, the ability to be enabled or disabled, the ability to hide or show it, the ability to dock or anchor, etc.

Creating the List of Items

Probably the most important aspect of a combo box is the list of items it holds. Like all other list-based controls, the list of a combo box is held by the Items property of the ComboBox class. The Items property of the ComboBox class is created from the nested ObjectCollection class:

member Items : ComboBox.ObjectCollection with get

To create a list of items, you can (continuously) call the ObjectCollection.Add() method. Here is an example:

open System;
open System.Drawing;
open System.Windows.Forms;

let exercise : System.Windows.Forms.Form = new Form();

let lblTitle : Label = new Label();
lblTitle.Text <- "Academic Disciplines";
lblTitle.Location <- new Point(12, 12);
lblTitle.AutoSize <- true;
exercise.Controls.Add(lblTitle);

let cbxAcademicDisciplines : ComboBox = new ComboBox();
cbxAcademicDisciplines.Location <- new Point(12, 32);

cbxAcademicDisciplines.Items.Add "Natural sciences" |> ignore
cbxAcademicDisciplines.Items.Add "Mathematics and Computer sciences" |> ignore
cbxAcademicDisciplines.Items.Add "Social sciences" |> ignore
cbxAcademicDisciplines.Items.Add "Humanities" |> ignore
cbxAcademicDisciplines.Items.Add "Professions and Applied sciences" |> ignore

exercise.Controls.Add(cbxAcademicDisciplines);

do
    System.Windows.Forms.Application.Run(exercise);

This would produce:

Combo Box

To add an array of items, you can call the AddRange() method. Here is an example:

open System;
open System.Drawing;
open System.Windows.Forms;

let exercise : System.Windows.Forms.Form = new Form();

let lblTitle : Label = new Label();
lblTitle.Text <- "Academic Disciplines";
lblTitle.Location <- new Point(12, 12);
lblTitle.AutoSize <- true;
exercise.Controls.Add(lblTitle);

let cbxAcademicDisciplines : ComboBox = new ComboBox();
cbxAcademicDisciplines.Location <- new Point(12, 32);
 
cbxAcademicDisciplines.Items.AddRange [| "Natural sciences";
					 "Mathematics and Computer sciences";
					 "Social sciences";
					 "Humanities";
					 "Professions and Applied sciences" |];

exercise.Controls.Add(cbxAcademicDisciplines);

do
    System.Windows.Forms.Application.Run(exercise);

To insert an item somewhere inside the list, you can call the Insert() method.

If you want the list of items to be sorted, you can set the value of the Sorted property from false (the default) to true. You can un-sort the list by changing the value of the Sorted property.

Selecting an Item

To select an item from the list, the user can click it. To programmatically select an item, you can assign a string to the Text property of a DropDown or a Simple combo box. Probably the best way to select an item is to specify its index. The items of a combo box are stored in a zero-based list. To select an item, you can assign its position to the SelectedIndex property. In the same way, to find out what item is selected, you can get the value of the SelectedIndex property.

Instead of using the index of an item, to select an item using its identity or name, you can use the SelectedItem property. To select an item by its name, assign it to the SelectedItem property.

Finding a String in the Combo Box

Instead of simply selecting an item from a combo box, the user may want to find out if a certain string exists in the  list. To support this operation, the ComboBox class is equipped with a method named FindString that is overloaded with two versions. One of the syntaxes of this method is:

member FindString : 
        s:string -> int;

This method takes as argument the string to find in the combo box. If the item is found in the list, the method returns its position. If the list does not have that string, the method return -1. The above syntax of the method would look through the whole list. If you want the search to start at a specific index, you can use the following version of the FindString() method:

member FindString : 
        s:string * 
        startIndex:int -> int

This version takes as the first argument a string. Instead of start looking for it from the beginning of the list, this method starts at the index specified by the startIndex value.

The FindString() method performs its operation without regards to case. This means that it would perform the same search for BlindMan, Blindman, blindMan, or BLINDMAN and would produce the same result for them. If you want the case of the characters to be taken into consideration, use the FindStringExact() method that also is overloaded with two versions. The syntax of the first version is:

member FindStringExact : 
        s:string -> int

This method proceeds like the FindString() method by starting to look for the string from the beginning of the list. If you want to specify from where to start looking for the string, you should use the following version:

member FindStringExact : 
        s:string * 
        startIndex:int -> int
     
 

The Styles of a Combo Box

 

The Flat Styles

Like most graphical controls, a combo box appears as a 3-D object with raised borders. As an alternative, you can display it as a flat object. To assist you with this choice, the ComboBox class provides the FlatStyle property. The FlatStyle property is based on the FlatStyle enumeration. Its members are:

  • Standard: This is the default value of the property. It makes the control appear with raised borders:
    open System;
    open System.Drawing;
    open System.Windows.Forms;
    
    let exercise : System.Windows.Forms.Form = new Form();
    
    let lblTitle : Label = new Label();
    lblTitle.Text <- "Academic Disciplines";
    lblTitle.Location <- new Point(12, 12);
    lblTitle.AutoSize <- true;
    exercise.Controls.Add(lblTitle);
    
    let cbxAcademicDisciplines : ComboBox = new ComboBox();
    cbxAcademicDisciplines.Location <- new Point(12, 32);
    cbxAcademicDisciplines.Width <- 232;
     
    cbxAcademicDisciplines.Items.AddRange [| "Natural sciences";
                                             "Mathematics and Computer sciences";
                                             "Social sciences";
                                             "Humanities";
                                             "Professions and Applied sciences" |];
    
    cbxAcademicDisciplines.FlatStyle <- FlatStyle.Standard;
    
    exercise.Controls.Add(cbxAcademicDisciplines);
    
    do
        System.Windows.Forms.Application.Run(exercise);

  • Popup: The control will appear flat with a surrounding gray line:
    cbxAcademicDisciplines.FlatStyle <- FlatStyle.Popup;

    Popup Combo Box

  • Flat: The control appears flat with a white surroundiong border:
    cbxAcademicDisciplines.FlatStyle <- FlatStyle.Flat;

  • System: The user's operating system (and theme, if any) will determine how the control must appear

The Drop Down Style

In our introduction to the combo box, we saw that it appeared like a text box with a down-pointing button on its right side. In reality, that was the description of just one type of combo box. There are three styles of combo boxes, although all allow the user to make only one selection. These styles are controlled by the DropDownStyle property, which is based on the ComboBoxStyle enumeration.

One of the types of combo boxes is referred to as Drop Down and is created by setting the DropDownStyle property to DropDown. Here is an example:

open System;
open System.Drawing;
open System.Windows.Forms;

let exercise : System.Windows.Forms.Form = new Form();

let lblTitle : Label = new Label();
lblTitle.Text <- "Academic Disciplines";
lblTitle.Location <- new Point(12, 12);
lblTitle.AutoSize <- true;
exercise.Controls.Add(lblTitle);

let cbxAcademicDisciplines : ComboBox = new ComboBox();
cbxAcademicDisciplines.Location <- new Point(12, 32);
 
cbxAcademicDisciplines.Items.AddRange [| "Natural sciences";
                                         "Mathematics and Computer sciences";
                                         "Social sciences";
                                         "Humanities";
                                         "Professions and Applied sciences" |];

cbxAcademicDisciplines.DropDownStyle <- ComboBoxStyle.DropDown;

exercise.Controls.Add(cbxAcademicDisciplines);

do
    System.Windows.Forms.Application.Run(exercise);

This type is made of a text box on the left side and a down-pointing arrowed button on the right side. Depending on how the control was created, when it comes up, it may not display anything:

A Drop Down Combo Box

Normally, if you want a DropDown style of combo box to display a string when the control comes up, you can either enter a value in the Text property or assign a string to the ComboBox.Text property. Here is an example:

open System;
open System.Drawing;
open System.Windows.Forms;

let exercise : System.Windows.Forms.Form = new Form();

let lblTitle : Label = new Label();
lblTitle.Text <- "Academic Disciplines";
lblTitle.Location <- new Point(12, 12);
lblTitle.AutoSize <- true;
exercise.Controls.Add(lblTitle);

let cbxAcademicDisciplines : ComboBox = new ComboBox();
cbxAcademicDisciplines.Location <- new Point(12, 32);
cbxAcademicDisciplines.DropDownStyle <- ComboBoxStyle.DropDown;
 
cbxAcademicDisciplines.Items.AddRange [| "Natural sciences";
                                         "Mathematics and Computer sciences";
                                         "Social sciences";
                                         "Humanities";
                                         "Professions and Applied sciences" |];

cbxAcademicDisciplines.Text <- "Social sciences";

exercise.Controls.Add(cbxAcademicDisciplines);

do
    System.Windows.Forms.Application.Run(exercise);

This would produce:

The Text of a Combo Box

The string you give to the Text property does not have to be one of the items of the list. 

To use the combo box, the user can click its down pointing arrow. At any time, to find out whether the list is displaying, you can check the value of the DroppedDown Boolean property. In the same way, to drop the list, you can set the combo box' DroppedDown property to true.

Once the list is displaying, if the user clicks that arrow, a list would appear (or expand). If the string assigned to the Text property is one of the items in the list, it would display in the text box side of the control and it would be selected in the list. Here is an example:

Using a DropDown combo box

If the string assigned to the Text property is not one of the items in the list, it would still appear. Here is an example:

open System;
open System.Drawing;
open System.Windows.Forms;

let exercise : System.Windows.Forms.Form = new Form();

let lblTitle : Label = new Label();
lblTitle.Text <- "Academic Disciplines";
lblTitle.Location <- new Point(12, 12);
lblTitle.AutoSize <- true;
exercise.Controls.Add(lblTitle);

let cbxAcademicDisciplines : ComboBox = new ComboBox();
cbxAcademicDisciplines.Location <- new Point(12, 32);
cbxAcademicDisciplines.DropDownStyle <- ComboBoxStyle.DropDown;
 
cbxAcademicDisciplines.Items.AddRange [| "Natural sciences";
                                         "Mathematics and Computer sciences";
                                         "Social sciences";
                                         "Humanities";
                                         "Professions and Applied sciences" |];

cbxAcademicDisciplines.Text <- "Arts & Sciences";

exercise.Controls.Add(cbxAcademicDisciplines);

do
    System.Windows.Forms.Application.Run(exercise);

This would produce:

When the list displays, either because the user clicked the arrow button, pressed Alt + the down arrow key, or because you decided to display it, the control fires a DropDown event, which is of type EventArgs.

If the user sees an item that he or she wants or was asked to select, he or she can click it. After an item has been clicked, two things happen: 1. the list retracts (or collapses) like a plastic; 2. the item that was clicked fills the text part and becomes the new selection:

Selecting an existing item in a DropDown combo box After selecting an item from a DropDown combo box

On the other hand, after displaying the list, if the user doesn't want to select anything from the list, he or she can click the arrow again or click anywhere away from the list. The list would collapse and the text part would get back to the previous way it was.

The Drop Down List

Another style of a combo box is gotten by setting the DropDownStyle to DropDownList. This type also is made of a text box on the left and a down-pointing arrowed button on the right side. It also may appear empty when it comes up, depending on how it was created. The biggest difference between a DropDown combo box and a DropDownList combo box is that, with the drop down list, the user can only select from the list: he or she cannot type anything in the text box part of the control.

Once again, to use the control, the user can click its arrow, which causes the list to display. The user can also display the list using the keyboard by pressing Alt + down arrow key after giving focus to the control.

The Simple Combo Box

The last type of combo box is called a simple combo box and is gotten by setting the DropDownStyle to Simple. After setting this value, you must heighten the control to get the desired size. This type of combo box is also made of two parts but they are distinct. The top section of the combo box displays a text box. Immediately under the text box, there is a list box. The following is the Character dialog box of OpenOffice.org. Its Font property page is equipped with the Font, the Typeface, and the Size combo boxes that are of a Simple style:

The Simple Combo Box

Notice that the control doesn't display a down-arrow pointing button on the right side of the selected item since the list is available already. To use this combo box, the user can examine the list part. If he or she sees the desired item, he can click it. When an item is clicked, it becomes the string of the top text part. If the user clicks a different item, it would become the new selection, replacing the one that was in the text part. Although this appears as a list box, the user cannot select more than one item.

The Ower-Draw Combo Box

The most regularly used combo boxes are made of text items. You can also create a combo box that displays colors or pictures. To create such a combo box, you start by changing the value of the DrawMode property that is set to Normal by default. If you want to display items that are not just regular text, you can set this property to either OwnerDrawFixed, which would make all items have the same height, or OwnerDrawVariable, which allows different items to have different sizes.

The Maximum Number of Drop-Down Items

If the combo box has a DropDownStyle other than Simple, there is typically a fixed number of items that display when the user clicks the control's arrow. You can control the number of items that displays using the MaxDropDownItems property. By default, this is set to 8. If the list contains a number of items less than the MaxDropDownItems integer value, all of the items would display fine. If the list contains more than the MaxDropDownItems number of items, when the user clicks the arrow, a vertical scroll box would appear. The control would display MaxDropDownItems number of items; to reveal more, the user would have to scroll in the list.

Automatic List Creation

 

Using an External List

 

In previous sections, we saw how to create a list of items. The .NET Framework provides an alternative. Instead of creating a list from scratch, you can use one that exists already. For example, you can use a list of recently accessed web sites or custom list of your own. To assist you with this, the ComboBox class provides three techniques.

To specify an external list of items to use for the combo box, you have two options. You can use the AutoCompleteSource property that is based on the AutoCompleteSource enumeration. The members of this enumeration are: None, RecentlyUsedList, FileSystem, FileSystemDirectories, HistoryList, ListItems, AllSystemSources, AllUrl, and CustomSource. Imagine that you want to use the list of web pages you had visited lately. To use that list, you can specify the AutoCompleteSource as HistoryList.

After specifying the source of the list, use the AutoCompleteMode property to specify how the combo box (or rather the text box side of the control) will assist the user. This property is based on the AutoCompleteMode enumeration that has four members. None is the default value. Imagine you had set the value of the AutoCompleteSource property as HistoryList. If you specify AutoCompleteMode as:

  • Suggest: In the text box part of the combo box, the user can click and start typing. A list of closely-matched items would display:
     


    In this case, as soon as the user types h, a list of URLs that start with h (for http) would come up. Once the user sees the desired item, he or she can then click that item to select it. Since there are many items, to continuously narrow the list, the user can keep typing until the desired item comes up
  • Append: In the text box part, the user can start typing. The control would then start looking for the closest matches and try to complete the user's entry with those available. Here is an example:
     

    First the user types h and http:// comes up as the first closest match. Then, the user specifies that the address starts with m and the compiler suggests, in alphabetical order, the closest URL with that. Then, the user types ms and finds out that msdn2 is available

  • SuggestAppend: This is a combination of the previous two options. When the control comes up, the user can start typing. The control would then display the list of items that start with what the user typed and it would display the starting closest match
     

    The user can continue typing. If the desired item appears in the list, the user can select it. Otherwise, as the user is typing, the closest match displays in the text box part of the control

    Combo Box Combo Box

Using a Custom List

 

Instead of using an external list, you can create your own. To do this, use the AutoCompleteCustomSource property. At design time, to create a list of strings, access the Properties window for the text box. In the Properties window, click the ellipsis button of the AutoCompleteCustomSource field to open the String Collection Editor. Enter the strings separated by a hard Return, and click OK. You can also programmatically create the list. To assist you, the .NET Framework provides a class named AutoCompleteStringCollection. The AutoCompleteStringCollection class implements the IList, the ICollection, and the IEnumerable interfaces.

After creating the custom list, to let the combo box use it, set the AutoCompleteMode property to CustomSource.

   
 

Home Copyright © 2015, FunctionX Home