Monday, April 29, 2013

Visual C# Lesson 3: Using ListBoxes, arrays, and the string.Split( ) method

Introduction


In this tutorial, I'll go over another user interface ListBox control, which allows us to display items in a scrollable or drop-down style list.  We will also see how to split a string into separate strings that are stored in a string array, using the Split( ) method.

The ListBox and TextBox Controls

For this project, you should do the following:
  1. Create a new Windows Forms Application named Lesson3
  2. Resize the form so it is a little bit wider
  3. Place the following controls onto the form:
  • ListBox
  • TextBox
  • Button
ListBox
For this control, do the following:
  • Change the (Name) property to lstNameBox
  • Resize the height of listbox to make it a little bit taller than the default
TextBox
For the textbox, do the following:
  • Change the (Name) property to txtInput
  • Resize the width of the textbox so it is about as long as the form

Button
  • Change the (Name) property to btnPerformAction
  • Change the Text property to Perform Action
  • Make the button longer so it can accomodate the text

Programming the Event Handler

Again, we're going to make use of an event handler, which will respond to the button's Click event.  In the Design View, double click the btnPerformAction button to generate the event handler, and show you the Code View.

I'll show you the code, and then we'll discuss it.  Try to see if you can understand what's going on.  Pay close attention to the array declaration and the use of the for loop.


       private void btnPerformAction_Click(object sender, EventArgs e)
        {
            string[] nameArray;

            nameArray = txtInput.Text.Split();

            for (int i = 0; i < nameArray.Length; i++)
            {
                lstNameBox.Items.Add(nameArray[i]);
            }//end for
        }

First, look at the array declaration.  Notice that it is slightly different than an array declaration in a language like C++.  You put the brackets, [ ], after the data type instead of after the identifier.

Below that, we use the string class's Split( ) method on the txtInput's Text property, which, by default splits the large text string into individual strings, delimited by spaces.

The for loop is very interesting.  In C#, arrays are classes and have properties.  The Length property is extremely useful, because it tells us how many items are in the array.

Inside the for loop, we access the lstNameBox's Items property, which is a collection, an array-like structure that can hold items in its own right.  Item-by-item, we call the Add(  ) method of the Items property, to add these individual items to the list box.

Running the Program - Round 1

To use this program, simply type a list of names, separated by spaces, in the textbox.  Then, click the Perform Action button.  Watch the magic.

 

It is pretty straightforward, but there is what could be considered a simple bug.  Click the button more than once and observe what happens.  It adds the list multiple times.

Fixing the Multiple Listing Bug and Running the Program - Round 2


So, how do we remedy this so that only what is in the textbox is split and displayed once?  We use the Clear( ) method of the listbox's Items collection each time before we enter the for loop.  Let's see our new code.


    private void btnPerformAction_Click(object sender, EventArgs e)
        {
            string[] nameArray;

            nameArray = txtInput.Text.Split();

            lstNameBox.Items.Clear();
            for (int i = 0; i < nameArray.Length; i++)
            {
                lstNameBox.Items.Add(nameArray[i]);
            }//end for
        }

Run the program again and try clicking the button multiple times.

This solves the problem.  One simple line of code.  We tell the listbox's Items property to empty itself before we go into the loop to add the items from the text box.

Summary

In this tutorial, we got our feet wet with yet another Windows Forms control.  This time, the ListBox.  We learned how to add elements to the listbox's Items property, which is a member of the ListBox class, and is an object collection.  Clearing the list was a piece of cake, with only one line of code being necessary.

We also learned about arrays - how to declare the array, and then assign it to another array.  In this case, the Split( ) method of the string class returned an array for us.

No comments:

Post a Comment