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.

 

  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with ListBox Example
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with Main and click OK
  11. 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 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;
    }
  12. Test the application and return to MSVC
  13. 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
    	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;
    }
  14. Test the application and return to MSVC
  15. To allow the user to perform multiple selections, change the source file as follows:
     
    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);
    
    	. . .
    }
  16. Test the application
 

Home Copyright © 2002-2005 FunctionX, Inc.