ListBox and ComboBox Sample

To get samples and instructions for installing them, see the following:

  • Click Samples on the Visual Studio Help menu.

    For more information, see Locating Sample Files.

  • The most recent versions and the complete list of samples are available on the Visual Studio 2008 Samples Web site.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied to a folder under \Program Files\Visual Studio 9\Samples\. For Visual Studio Express Editions, all samples are located on the Internet.

To run this sample

  • Press F5.

Requirements

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

Demonstrates

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 whole 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 enable you to specify some of the layout properties that affect the behavior of the ComboBox control.