A combo box is a control that maintains a list of
objects but displays one at a time to the user. A regular combo box is
equipped with a rectangular text area and a down-pointing arrow. To use
the combo box, the user clicks the arrow, which displays its list of
items. Then the user clicks one item from the list and the list retracts
like a plastic. The item the user would have selected would display in the
text box side of the control. To perform a different selection, the user
uses the same process. Practically, there are different variations of the
combo box control depending on the programmer's goal.
To create a combo box in MS .Net application, you can use
the ComboBox class.
Creating a combo box might not be the most difficult
thing to do but when the user has performed a different selection, you
will usually need to know what item the user selected. A combo box
maintains its list of items by their text or their indexes. You can use
the same ability to retrieve either the text or the index of the item that
was selected. |
|
- Start Microsoft Visual Studio .NET
- On the Start Page, click New Project (alternatively, on the main
menu, you can click File -> New -> Project...)
- On the New Project dialog box, in the Project Types tree list, click
Visual C++ Projects
- In the Templates list, click Managed C++ Empty Project
- In the Name edit box, replace the <Enter name> content with ComboBox
Example
- In the Location combo box, accept the suggestion or type your own.
If you don't have any, type C:\Programs\MSVC.NET
- Click OK
- On the main menu, click Project -> Add New Item...
- In the Add New Item dialog box, in the Templates list, click C++
File
- In the Name box, replace <Enter name> with Main and click OK
- Replace the contents of the empty file with the following:
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
__gc class SimpleForm : public Form
{
public:
SimpleForm();
private:
// Declare an instance to a ComboBox class
ComboBox *cboCountries;
};
SimpleForm::SimpleForm()
{
// The caption of the form
this->Text = S"Combo Box Example";
// Use the instance of the combo box to initialize the class
cboCountries = new ComboBox;
// Specify where to position the combo box
cboCountries->Location = Point(16, 16);
// Add each item to the combo box
cboCountries->Items->Add(S"Gabon");
cboCountries->Items->Add(S"Senegal");
cboCountries->Items->Add(S"Botswana");
cboCountries->Items->Add(S"Zambia");
cboCountries->Items->Add(S"Lesotho");
// Specify which item to display when the combo box comes up
cboCountries->Text = S"Senegal";
// After creating the control, add it to the
// group of controls of the form
this->Controls->Add(cboCountries);
}
int __stdcall WinMain()
{
SimpleForm *SF = new SimpleForm();
Application::Run(SF);
return 0;
}
|
- Test the application and return to MSVC
- To retrieve the text of the item that was selected and display it in
a label, change the source file as follows:
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
__gc class SimpleForm : public Form
{
public:
SimpleForm();
private:
Label *lblItemSelected;
// Declare an instance to a ComboBox class
ComboBox *cboCountries;
void SelectionChanged(Object *Sender, EventArgs *Args);
};
SimpleForm::SimpleForm()
{
// The caption of the form
this->Text = S"Combo Box Example";
// Use the instance of the combo box to initialize the class
cboCountries = new ComboBox;
// Specify where to position the combo box
cboCountries->Location = Point(16, 16);
// Add each item to the combo box
cboCountries->Items->Add(S"Gabon");
cboCountries->Items->Add(S"Senegal");
cboCountries->Items->Add(S"Botswana");
cboCountries->Items->Add(S"Zambia");
cboCountries->Items->Add(S"Lesotho");
// Specify which item to display when the combo box comes up
cboCountries->Text = S"Senegal";
cboCountries->add_SelectedIndexChanged(new EventHandler(this, SelectionChanged));
// After creating the control, add it to the
// group of controls of the form
this->Controls->Add(cboCountries);
lblItemSelected = new Label;
lblItemSelected->Location = Point(16, 48);
lblItemSelected->AutoSize = true;
lblItemSelected->Text = S"Senegal";
this->Controls->Add(lblItemSelected);
}
void SimpleForm::SelectionChanged(Object *Sender, EventArgs *Args)
{
lblItemSelected->Text = cboCountries->Text;
}
int __stdcall WinMain()
{
SimpleForm *SF = new SimpleForm();
Application::Run(SF);
return 0;
}
|
- Test the application
|
|