Home

Characteristics of a List Box

 
 

The Scroll Bars

If you provide a longer list than the list box' height can display, it would have a vertical scroll bar. Here is an example:

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();
        lbxFamily.Location = new Point(12, 12);
        lbxFamily.Items.Add("Son");
        lbxFamily.Items.Add("Daughter");
        lbxFamily.Items.Add("Father");
        lbxFamily.Items.Add("Mother");

        string[] strMembers =
        {
            "Niece", "Nephew", "Uncle", "Aunt",
            "Grand Father", "Grand Mother"
        };
        lbxFamily.Items.AddRange(strMembers);

        Controls.Add(lbxFamily);
    }
}

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

This would produce:

A List Box With a Vertical Scroll Bar

At design time, if just one or a few items are hidden by the scroll bar, you can heighten it if the form provides more space.

Consider the following example:

public class Exercise : System.Windows.Forms.Form
{
    ListBox lbxBook;
    Label lblTitle;

    public Exercise()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        lblTitle = new Label();
        lblTitle.Text = "Book Titles";
        lblTitle.Location = new Point(12, 12);

        lbxBook = new ListBox();
        lbxBook.Location = new Point(12, 36);
        lbxBook.Items.Add("College Algebra");;
        lbxBook.Items.Add("Finite Mathematics");
        lbxBook.Items.Add("Mathematical Structures");
        lbxBook.Items.Add("MCAD 70-316 Training Guide");
        lbxBook.Items.Add("C++ Builder 6 Developer's Guide");

        Controls.Add(lblTitle);
        Controls.Add(lbxBook);
    }
}

This would produce:

A list box with wide items

If at least one of the items of the list box is wider than the width of the control, the right side(s) of that (those) may disappear. To allow the user to see the hidden part of the item(s), you should display a horizontal scroll bar. To support this, the ListBox class is equipped with a Boolean property named HorizontalScrollbar. To make a list box display a horizontal scroll bar, at design time, access the Properties window for the list box and set its HorizontalScrollbar property to True. You can also do this programmatically. Here is an example:

private void InitializeComponent()
{
        lblTitle = new Label();
        lblTitle.Text = "Book Titles";
        lblTitle.Location = new Point(12, 12);

        lbxBook = new ListBox();
        lbxBook.Location = new Point(12, 36);
        lbxBook.Items.Add("College Algebra");;
        lbxBook.Items.Add("Finite Mathematics");
        lbxBook.Items.Add("Mathematical Structures");
        lbxBook.Items.Add("MCAD 70-316 Training Guide");
        lbxBook.Items.Add("C++ Builder 6 Developer's Guide");
        
        bxBook.HorizontalScrollbar = true;

        Controls.Add(lblTitle);
        Controls.Add(lbxBook);
}

This property allows the operating system to find the widest item in the list and provide a horizontal scroll bar that is long enough to display each item when the user scrolls to the right. The above code would produce:

A List Box With a Horizontal Scroll Bar

If the list of items requires it, the list box would display both the vertical and the horizontal scroll bars. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    ListBox lbxBook;
    Label lblTitle;

    public Exercise()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        lblTitle = new Label();
        lblTitle.Text = "Book Titles";
        lblTitle.Location = new Point(12, 12);

        lbxBook = new ListBox();
        lbxBook.Location = new Point(12, 36);
        lbxBook.Items.Add("College Algebra");;
        lbxBook.Items.Add("Finite Mathematics");
        lbxBook.Items.Add("Mathematical Structures");
        lbxBook.Items.Add("MCAD 70-316 Training Guide");
        lbxBook.Items.Add("C++ Builder 6 Developer's Guide");
        lbxBook.Items.Add("La Bible de JÚrusalem");
        lbxBook.Items.Add("Patterns for a Purpose");
        lbxBook.HorizontalScrollbar = true;

        Controls.Add(lblTitle);
        Controls.Add(lbxBook);
    }
}

This would produce:

If you prefer to decide how much width should be allowed, then set the desired value in the HorizontalExtent property.

Practical Learning Practical Learning: Implementing List Boxes

  1. Display the form and click the most right list box. Using the Properties window, set its HorizontalScrollbar property to True
  2. Execute the application and process an order
  3. After using it, close the form and return to your programming environment

A Multi-Column List Box

When you create a list of items, they appear in one column. If the number of items exceeds the height, a scrollbar would appear on the control. An alternative you can use is to span the list to more than one column. To support this, the ListBox class is equipped with the MultiColumn Boolean property. At design time, you can set this characteristic in the Properties window. By default, the MultiColumn value is set to False, which means the items appear in one column. If you set this property to True, then the compiler would decide if or when the control needs the columns, based on the number of items in the list. You can then specify the width of each column using the ColumnWidth property.

A Custom Owner-Draw List Box

A list box is painted based on three types or style. This characteristic is controlled by the DrawMode property. When its value is set to Normal, the operating system would regularly draw each item of the list. If you want each item of the list to display a graphic or a color, you must set the style to an owner drawn type. The OwnerDrawFixed value allows you to set a desired but same height for each item of the list. This height is controlled through the ItemHeight property. You can set a different height for each item if you set the list style to OwnerDrawVariable.

 

Previous Copyright © 2007 FunctionX, Inc. Home