This documentation is archived and is not being maintained.

ListBox and ComboBox Sample 

Download sample

This sample demonstrates several basic ListBox and ComboBox tasks, including adding items, binding to a database table, and accessing the selected item.

Security noteSecurity Note

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

  1. Click Download Sample.

    The File Download message box appears.

  2. Click Open, and on the left column of the zip folder window, click Extract all files.

    The Extraction Wizard opens.

  3. Click Next. You can change the directory that the files will be extracted to, and then click Next again.

  4. Make sure that the Show extracted files check box is selected, and click Finish.

  5. 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

  • Press F5.


This sample requires the Northwind database. For more information, see How to: Install and Troubleshoot Database Components for Samples.


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. The Process objects are added to the Items collection of the ListBox controls. The DisplayMember 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 a DataTable with 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 the DataSource of the ListBox control. The ValueMember 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.

See Also