ListBox and ComboBox Sample
This sample demonstrates several basicand tasks, including adding items, binding to a database table, and accessing the selected item.
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
To open the sample file in Solution Explorer
Click Download Sample.
The File Download message box appears.
Click Open, and on the left column of the zip folder window, click Extract all files.
The Extraction Wizard opens.
Click Next. You can change the directory that the files will be extracted to, and then click Next again.
Make sure that the Show extracted files check box is selected, and click Finish.
Double-click the sample's .sln file.
The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.
To run this sample
This sample requires the Northwind database. For more information, see.
The tabbed pages of the tab control demonstrate different techniques:
Add Items The sample retrieves an array of Process objects representing the processes currently running on the computer. Theobjects are added to the collection of the ListBox controls. The property of the ListBox control is used to specify which property of the Process object is displayed in the ListBox control.
Bind to DataTable This tab page has code to fill awith a list of all files in your My Documents folder, and then binds the list box to that DataTable.
Bind to Array This tab page has code similar to the Add Items page. Instead of adding the Process objects one by one to the Items collection, the entire array of Process objects is used as theof the ListBox control. The and DisplayMember properties of the ListBox control are used to control the display and retrieval of the items.
ComboBox This tab page has code to bind a ComboBox control to a DataTable retrieved from SQL Server using the new TableAdapter and typed dataset features. This example expects that it can find the Northwind sample database in SQL Server on the local computer. The page has controls to allow you to specify some of the layout properties that affect the behavior of the ComboBox control.