Home » C# Basics » 08 - Windows Programming - 1
8

ListBox Control

Defines the properties of the listbox control in a windows application

The ListBox control is used for presenting the user with a list of options for selection. Depending upon the property of the listbox, users can select one or more items from it.

Some commonly used properties of the ListBox control are:

SelectedItem

Contains the selected item from the listbox.

SelectedIndex

Zero based index of the selected item in the listbox.

Items

Collection of all the items in the listbox. Used for adding or removing items from the list using the Add() and Remove() methods.

Sorted

Indicates if the items in the list are sorted or not.

SelectionMode

None - no item can be selected.

One - only one item can be selected.

MultiSimple -multiple items can be selected by clicking them with mouse.

MultiExtended -multiple items can be selected using the Ctrl, Shift and arrow keys.

Let us make an application to understand the working of the ListBox control:

1. Start a new Windows Application project. On the form, place two label controls, two listbox controls and a button control as shown in the figure:

2. Using the table, set the properties of the form and the controls:

Property

Value

Form

Text

StartPosition

MaximizeBox

FormBorderStyle

List Box Demo

Center Screen

False

FixedSingle

label1

Text

Available Skills

label2

Text

Your Skills

button1

(Name)

Text

btnAdd

Add Skill

listBox1

(Name)

SelectionMode

Sorted

lboxASkills

One

True

listBox2

(Name)

SelectionMode

lboxYSkills

One

3. Select the first listbox (lboxASkills) and in the properties window, look for the property named - Items. In the cell next to it (with the word `Collection' in it), there is a small button. Click on the button.

4. A dialog box will open up. This is where we can specify the list of items that will appear in the listbox. Enter a list of some items. For this application, a list of programming skills is used. Enter one item per line. Click the OK button when the list is complete.

5. The `Available Skills' listbox will now display the items that we have entered in the previous state. The ai of the application is obvious now. The user can select the skills from this list box and add them to the `Your Skills' listbox by clicking the `Add Skill' button.

6. Double click on the `Add Skill' button to open up its Click event handler in the code view and type the following code in it:

private void btnAdd_Click(object sender, EventArgs e) { if (lboxASkills.SelectedItem == null) { MessageBox.Show("Select a skill to add"); } else { int found; found = lboxYSkills.FindStringExact(lboxASkills.SelectedItem.ToString());
if (found == -1) { lboxYSkills.Items.Add(lboxASkills.SelectedItem); }
else { MessageBox.Show("Skill already added"); } } }

Let us understand the code. The first `if' statement is used to check if the user has selected an item from the `Available Skills' list box before clicking the button. The statement - lboxASkills.SelectedItem==null - will return `true' if the user has not selected any skill before clicking the button. In this case, a message box will be displayed with the text - `Select a skill to add'. This check is vital because if do not put this code and the user clicks the button without selecting any skill, the application may crash.

However, if the user selects a skill and then clicks the button, the condition becomes `false' and the program will enter the `else' part. Here, we have declared an integer variable `found'. In the next statement, we are determining if the skill being added by the user already exists in the `Your Skills' listbox because we do not want to allow duplicate skills. To do this, we have to use the FindStringExact() method of the listbox. This method tests if a string exists in the listbox and returns -1 if the string is not found. The lboxASkills.SelectedItem.ToString() method will convert the selected skill from the `Available Skills' list box into a string and the lboxYSkills.FindStringExact() method will find this string in the `Your Skills' listbox. If not found, it will return -1 and this value will be stored in the `found' variable. This indicates that the skill has not been added so far.

The next `if' statement checks the value of the variable `found'. If this value is -1 i.e. the skill is not present in the `Your Skills' listbox, the lbox.Items.Add() method will add the selected skill (which is the selected item of the `Available Skills' listbox, referred as - lboxASkills.SelectedItem) to the listbox. If the value of `found' is not -1, it means that the skill is already in the listbox and the user will get a message - Skill already added.

Here are some sample runs of the application. The first image shows the message when the `Add Skill' button is clicked without selecting any skill from the `Available Skills' listbox -

Some skills being added to the `Your Skills' listbox:

To add a duplicate skill:-

In this chapter, we learned the basics of windows programming - properties, events, form and controls. In the next chapter, we will explore some advanced concepts of windows application development.