Visual C#

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…

3 min read552 words1 code sample

Description#

A list box offers a selection of items, each shown on a separate line. A user selects an item by clicking it, causing the line to be highlighted as the active choice. Here is an example: while keeping the original behavior intact.

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 more readily 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: Introducing List Boxes while keeping the original behavior intact.

  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.

C#
using System;
using System.Drawing;
using System.Windows.Forms;

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

    public Exercise()
    {
        InitializeComponent();
    }

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

        Controls.Add(lbxFamily);
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

For the applications we develop, list-oriented controls will be named using plural forms, though this is a preference rather than a strict requirement or standard.

Practical Learning: Creating List Boxes

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

Home Copyright © 2008-2009 C# Key Next