Windows Control: The List Box



A list box presents a list of items to choose from. Each item displays on a line. The user makes a selection by clicking in the list. Once clicked, the item or line on which the mouse landed becomes highlighted, indicating that it is the current choice. Here is an example:

The Commands tab of the Customize dialog box of Microsoft Access

After an item has been selected, to make a different selection, the user would click another. The new clicked item becomes selected or highlighted; the previously selected item looses its highlighting attribute. The user can also change the selection by pressing the up or down arrow keys.

List boxes are categorized in two types: single and multi-selection. The second category allows a user to select more than one item by pressing Ctrl to select items at random or by pressing Shift to select items in a range.

One of the main reasons for using a list box is to display a list of items to the user. Sometimes the list would be very large. If the list is longer than the available client area of the control, the control would be equipped with a scroll bar that allows the user to navigate up and down to access all items of the list. You will have the option of deciding how many items to display on the list.

Practical Learning Practical Learning: Introducing List Boxes

  1. Start Microsoft Visual C# and create a new Windows Application named MusicalInstrumentStore1
  2. In the Solution Explorer, right-click Form1.cs and click Rename
  3. Type MusicStore.cs and press Enter

Creating a List Box

To support list boxes, the .NET Framework provides the ListBox class. At design time, to add a list box to an application, from the Common Controls section of the Toolbox, click the ListBox control and click the form or the control that will host it. To programmatically create a list box, declare a variable of type ListBox, use the new operator to allocate memory it, and add it to the Controls property of its eventual parent.

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
    ListBox lbxFamily;

    public Exercise()

    private void InitializeComponent()
        lbxFamily = new ListBox();


public class Program
    static int Main()
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
Author Note In our applications, the names of the list-based controls will be in plural. This is not a rule and it is not based on any preconceived standard.

Practical Learning Practical Learning: Creating List Boxes

  1. Design the form as follows:
    Musical Instrument Shop - Form Design
    Control Text Name Other Properties
    GroupBox GroupBox Musical Instrument Selection    
    Label Label Categories    
    Label Label Types    
    Label Label Items    
    ListBox ListBox   lbxCategories  
    ListBox ListBox   lbxTypes  
    ListBox ListBox   lbxItems  
    GroupBox GroupBox Selected Items    
    Label Label   Part #  
    Label Label   Description  
    Label Label   Unit Price  
    Label Label   Qty  
    Label Label   Sub Total  
    TextBox TextBox   txtPartID1  
    TextBox TextBox   txtDescription1  
    TextBox TextBox 0.00 txtUnitPrice1 TextAlign: Right
    TextBox TextBox 0 txtQantity1 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal1 TextAlign: Right
    Button Button Rmv btnRemove1  
    TextBox TextBox   txtPartID2  
    TextBox TextBox   txtDescription2  
    TextBox TextBox 0.00 txtUnitPrice2 TextAlign: Right
    TextBox TextBox   txtQuantity2 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal2 TextAlign: Right
    Button Button Rmv btnRemove2  
    TextBox TextBox   txtPartID3  
    TextBox TextBox   txtDescription3  
    TextBox TextBox 0.00 txtUnitPrice3 TextAlign: Right
    TextBox TextBox 0 txtQuantity3 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal3 TextAlign: Right
    Button Button Rmv btnRemove3  
    TextBox TextBox   txtPartID4  
    TextBox TextBox   txtDescription4  
    TextBox TextBox 0.00 txtUnitPrice4 TextAlign: Right
    TextBox TextBox   txtQuantity4 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal4 TextAlign: Right
    Button Button Rmv btnRemove4  
    TextBox TextBox   txtPartID5  
    TextBox TextBox   txtDescription5  
    TextBox TextBox 0.00 txtUnitPrice5 TextAlign: Right
    TextBox TextBox 0 txtQuantity5 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal5 TextAlign: Right
    Button Button Rmv btnRemove5  
    TextBox TextBox   txtPartID6  
    TextBox TextBox   txtDescription6  
    TextBox TextBox 0.00 txtUnitPrice6 TextAlign: Right
    TextBox TextBox 0 txtQuantity6 TextAlign: Right
    TextBox TextBox 0.00 txtSubTotal6 TextAlign: Right
    Button Button Rmv btnRemove6  
    Button Button Close btnClose  
    Label Label   Order Total:  
    TextBox TextBox 0.00 txtTotalOrder TextAlign: Right
  2. Save the form

Home Copyright © 2007 FunctionX, Inc. Next