Windows Control: The Combo Box
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…
Description#
Users can select options from a list using a combo box. This control is typically populated with strings, similar to a list box, but it is much more compact because it only occupies the same area as a standard text box. An arrow pointing downward on the right side of the control indicates the presence of a hidden list. For instance, the Font Effects property page in the OpenOffice.org Character dialog features several combo boxes, including those for Font Color, Relief, Strikethrough, Effects, Color, and Underlining:
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 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;
}
}Within the .NET Framework, combo box functionality is provided by the ComboBox class. You can add one to your form during the design phase by selecting the ComboBox icon from the Common Controls area of the Toolbox. Since ComboBox is a descendant of the ListControl class (just like ListBox), you can also instantiate it programmatically. To do so, define a ComboBox variable, initialize it using the new keyword, and then include it in the container's Controls collection. Consider this demonstration:
Practical Learning: Introducing Combo Boxes
- Start Microsoft Visual C# and create a new Windows Application named CollegeParkAutoParts1
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Central.cs and press Enter
- Design the form as follows: Control Text Name Other Properties GroupBox Parts Selection Label Year Font: Microsoft Sans Serif, 8.25pt, style=Bold Label Make Microsoft Sans Serif, 8.25pt, style=Bold Label Model Microsoft Sans Serif, 8.25pt, style=Bold Label Category Microsoft Sans Serif, 8.25pt, style=Bold ComboBox cbxCarYears ComboBox cbxMakes ComboBox cbxModels ComboBox cbxCategories Label Available Parts Font: Microsoft Sans Serif, 9.75pt, style=Bold ListBox lbxPartNumbers ListBox lbxPartNames ListBox lbxUnitPrices GroupBox Customer Selected Items Label Part # Label Part Name Label Unit Price Label Qty Label Sub Total TextBox txtPartNumber1 TextBox txtPartName1 TextBox 0.00 txtUnitPrice1 TextAlign: Right TextBox 0 txtQuantity1 TextAlign: Right TextBox 0.00 txtSubTotal1 TextAlign: Right Button Remove btnRemove1 TextBox txtPartNumber2 TextBox txtPartName2 TextBox 0.00 txtUnitPrice2 TextAlign: Right TextBox 0 txtQuantity2 TextAlign: Right TextBox 0.00 txtSubTotal2 TextAlign: Right Button Remove btnRemove2 TextBox txtPartNumber3 TextBox txtPartName3 TextBox 0.00 txtUnitPrice3 TextAlign: Right TextBox 0 txtQuantity3 TextAlign: Right TextBox 0.00 txtSubTotal3 TextAlign: Right Button Remove btnRemove3 TextBox txtPartNumber4 TextBox txtPartName4 TextBox 0.00 txtUnitPrice4 TextAlign: Right TextBox 0 txtQuantity4 TextAlign: Right TextBox 0.00 txtSubTotal4 TextAlign: Right Button Remove btnRemove4 TextBox txtPartNumber5 TextBox txtPartName5 TextBox 0.00 txtUnitPrice5 TextAlign: Right TextBox 0 txtQuantity5 TextAlign: Right TextBox 0.00 txtSubTotal5 TextAlign: Right Button Remove btnRemove5 TextBox txtPartNumber6 TextBox txtPartName6 TextBox 0.00 txtUnitPrice6 TextAlign: Right TextBox 0 txtQuantity6 TextAlign: Right TextBox 0.00 txtSubTotal6 TextAlign: Right Button Remove btnRemove6 Label Total Order TextBox 0.00 txtTotalOrder Button Close btnClose
- Execute the application to test it
- Close the form and return to your programming environment
Main Page Copyright © 2008-2009 C# Key Next

