Share via


ListBox and ComboBox Sample

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

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

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.

See Also

Concepts

What's New in Data

Reference

ListControl.DisplayMember

ListControl.ValueMember

ListBox.SelectedIndexChanged

ListBox.SelectedIndices

ListBox.SelectedItems

ListBox.SelectionMode

Process

Other Resources

ListBox Control (Windows Forms)

ComboBox Control (Windows Forms)