Visual C++ .Net Controls: The List Box |
|
A list box is a control that maintains a list of objects in a rectangular box. To use the list box, the user clicks one of its items. Depending on the list box, when an item is clicks, it becomes highlighted, indicating that it is selected. There are two types of selection list boxes: one type allows the user to select only one item, another type would allow the user to select more than one item. To create a list box in MS .Net application, you can use the ListBox class. Creating a list 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(s) the user selected. A list 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(s) that was (were) selected.
|
#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 ListBox class ListBox *lstCities; }; SimpleForm::SimpleForm() { // The caption of the form this->Text = S"List Box Example"; // Use the instance of the combo box to initialize the class lstCities = new ListBox; // Specify where to position the combo box lstCities->Location = Point(16, 16); // Add each item to the combo box lstCities->Items->Add(S"Cape Town"); lstCities->Items->Add(S"Rio de Janeiro"); lstCities->Items->Add(S"Le Caire"); lstCities->Items->Add(S"Amsterdam"); lstCities->Items->Add(S"Beijing"); lstCities->Items->Add(S"Santiago"); lstCities->Items->Add(S"Baghdad"); lstCities->Items->Add(S"Los Angeles"); lstCities->Items->Add(S"Sydney"); lstCities->Items->Add(S"Marseille"); // Specify which item to display when the combo box comes up lstCities->Text = S"Senegal"; // After creating the control, add it to the // group of controls of the form this->Controls->Add(lstCities); } int __stdcall WinMain() { SimpleForm *SF = new SimpleForm(); Application::Run(SF); return 0; } |
#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 ListBox *lstCities; 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 lstCities = new ListBox; // Specify where to position the combo box lstCities->Location = Point(16, 16); // Add each item to the combo box lstCities->Items->Add(S"Cape Town"); lstCities->Items->Add(S"Rio de Janeiro"); lstCities->Items->Add(S"Le Caire"); lstCities->Items->Add(S"Amsterdam"); lstCities->Items->Add(S"Beijing"); lstCities->Items->Add(S"Santiago"); lstCities->Items->Add(S"Baghdad"); lstCities->Items->Add(S"Los Angeles"); lstCities->Items->Add(S"Sydney"); lstCities->Items->Add(S"Marseille"); // Specify which item to display when the combo box comes up lstCities->Text = S"Amsterdam"; lstCities->add_SelectedIndexChanged(new EventHandler(this, SelectionChanged)); // After creating the control, add it to the // group of controls of the form this->Controls->Add(lstCities); lblItemSelected = new Label; lblItemSelected->Location = Point(160, 16); lblItemSelected->AutoSize = true; lblItemSelected->Text = S"Amsterdam"; this->Controls->Add(lblItemSelected); } void SimpleForm::SelectionChanged(Object *Sender, EventArgs *Args) { lblItemSelected->Text = lstCities->Text; } int __stdcall WinMain() { SimpleForm *SF = new SimpleForm(); Application::Run(SF); return 0; } |
SimpleForm::SimpleForm() { . . . lstCities->Text = S"Amsterdam"; lstCities->SelectionMode = SelectionMode::MultiExtended; lstCities->add_SelectedIndexChanged(new EventHandler(this, SelectionChanged)); // After creating the control, add it to the // group of controls of the form this->Controls->Add(lstCities); . . . } |
|
||
Home | Copyright © 2002-2005 FunctionX, Inc. | |
|