|
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: Introducing List Boxes
|
|
- Start Microsoft Visual C# and create a new Windows Application named MusicalInstrumentStore1
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type MusicStore.cs and press Enter
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()
{
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;
}
}
 |
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: Creating List Boxes
|
|
- 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 |
|
- Save the form
|