Home

Windows Control: The Combo Box

 

Description

A combo box is a list of items that the user can select from. Like a list box, a combo box is usually made of a list of strings. Unlike a list box, a combo box saves space by using just as much room as a text box control. To show that it is holding a list, a combo box displays a down-pointing arrow on the right side of its text box. 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 like a list box, 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. At design time, to add a combo box to your application, from the Common Controls section of the Toolbox, you can click the ComboBox button ComboBox and click the form or a container. Like ListBox, the ComboBox class is derived from the ListControl class. Therefore, to programmatically 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:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
    Label lblTitle;
    ComboBox cbxAcademicDisciplines;

    public Exercise()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        lblTitle = new Label();
        lblTitle.Text = "Academic Disciplines";
        lblTitle.Location = new Point(12, 12);
        lblTitle.AutoSize = true;
        Controls.Add(lblTitle);

        cbxAcademicDisciplines = new ComboBox();
        cbxAcademicDisciplines.Location = new Point(12, 32);

        Controls.Add(cbxAcademicDisciplines);
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

This would produce:

Combo Box

Practical Learning Practical Learning: Introducing Combo Boxes

  1. Start Microsoft Visual C# and create a new Windows Application named CollegeParkAutoParts1
  2. In the Solution Explorer, right-click Form1.cs and click Rename
  3. Type Central.cs and press Enter
  4. Design the form as follows:
     
    College Park Auto Parts - Form Design
    Control Text Name Other Properties
    GroupBox GroupBox Parts Selection    
    Label Label Year   Font: Microsoft Sans Serif, 8.25pt, style=Bold
    Label Label Make   Microsoft Sans Serif, 8.25pt, style=Bold
    Label Label Model   Microsoft Sans Serif, 8.25pt, style=Bold
    Label Label Category   Microsoft Sans Serif, 8.25pt, style=Bold
    ComboBox ComboBox   cbxCarYears  
    ComboBox ComboBox   cbxMakes  
    ComboBox ComboBox   cbxModels  
    ComboBox ComboBox   cbxCategories  
    Label Label Available Parts   Font: Microsoft Sans Serif, 9.75pt, style=Bold
    ListBox ListBox   lbxPartNumbers  
    ListBox ListBox   lbxPartNames  
    ListBox ListBox   lbxUnitPrices  
    GroupBox GroupBox Customer Selected Items    
    Label Label Part #    
    Label Label Part Name    
    Label Label Unit Price    
    Label Label Qty    
    Label Label Sub Total    
    TextBox TextBox   txtPartNumber1  
    TextBox TextBox   txtPartName1  
    TextBox TextBox 0.00 txtUnitPrice1 TextAlign: Right
    TextBox TextBox 0 txtQuantity1 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal1 TextAlign: Right
    Button Button Remove btnRemove1  
    TextBox TextBox   txtPartNumber2  
    TextBox TextBox   txtPartName2  
    TextBox TextBox 0.00 txtUnitPrice2 TextAlign: Right
    TextBox TextBox 0 txtQuantity2 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal2 TextAlign: Right
    Button Button Remove btnRemove2  
    TextBox TextBox   txtPartNumber3  
    TextBox TextBox   txtPartName3  
    TextBox TextBox 0.00 txtUnitPrice3 TextAlign: Right
    TextBox TextBox 0 txtQuantity3 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal3 TextAlign: Right
    Button Button Remove btnRemove3  
    TextBox TextBox   txtPartNumber4  
    TextBox TextBox   txtPartName4  
    TextBox TextBox 0.00 txtUnitPrice4 TextAlign: Right
    TextBox TextBox 0 txtQuantity4 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal4 TextAlign: Right
    Button Button Remove btnRemove4  
    TextBox TextBox   txtPartNumber5  
    TextBox TextBox   txtPartName5  
    TextBox TextBox 0.00 txtUnitPrice5 TextAlign: Right
    TextBox TextBox 0 txtQuantity5 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal5 TextAlign: Right
    Button Button Remove btnRemove5  
    TextBox TextBox   txtPartNumber6  
    TextBox TextBox   txtPartName6  
    TextBox TextBox 0.00 txtUnitPrice6 TextAlign: Right
    TextBox TextBox 0 txtQuantity6 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal6 TextAlign: Right
    Button Button Remove btnRemove6  
    Label Label Total Order    
    TextBox TextBox 0.00 txtTotalOrder  
    Button Button Close btnClose  
  5. Execute the application to test it
  6. Close the form and return to your programming environment
 

Home Copyright © 2007 FunctionX, Inc. Next