Software Design Using C++
Fourth Intermediate Windows Forms Example
The Goal
We wish to create a Windows App that displays the square roots of the first so many
positive integers. Let's make it so that the user can specify how many integers
are desired. Here is a picture of the resulting app showing 25 roots.
This picture of the app with 40 roots illustrates the fact that
the user can scroll through the list of roots if the list is too long to all be seen at once.
The particular control that we use to hold the integers and their roots is called a ListBox.
Getting Started
Create a new C++ or C# Windows Forms App project and name it Roots or similar.
Resize the resulting form so that it is about the same dimensions as seen in the
picture of the running application.
In the Properties Window, change the MaximizeBox property to false so that the user
will not be able to maximize the application. Change the Text property to
"List of Square Roots" (without the quotes). You should not need to change any other
properties for the overall form.
Adding the Controls
From the picture of the running app, you can see that we
need a label at the top left. Drag one from the Toolbox to your form and change its Text
property to "Number of ints to use:" (again without the quotes). To the right of it put a TextBox control
and change its name to NumberBox . This box will be used to hold the number of integers
for which we want square roots. To the right of this box put a button and change its name to
StartButton . Change its Text property to "Start". You have already used controls
like this in the previous examples and should have little trouble adding these controls.
Next, we turn to the new control, the so-called ListBox. Drag one of these from the Toolbox to
your form, just under the 3 controls already present. Resize it so that it fills most of the
section of the form below the 3 controls. Change the name of this control to RootsListBox .
Save all of your files. Also right click on the new project in Solution Explorer
and set this project to be the startup project. You might even want to build and run the project to
be sure that nothing went wrong so far.
The Code: C++ Version
In Design View, double click on the button on your form so as to produce an outline (function declaration)
for your click handler. In Form1.h change it to look like this:
#pragma endregion
private:
System::Void StartButton_Click(System::Object^ sender, System::EventArgs^ e);
};
}
|
Then, in Roots.cpp (or whatever your .cpp file is named) place the code for this new function. Use the following:
System::Void Form1::StartButton_Click(System::Object^ sender, System::EventArgs^ e)
{
long k, Count;
double Root;
String^ Info;
RootsListBox->Items->Clear();
Count = Convert::ToInt32(NumberBox->Text);
for (k = 1; k <= Count; k++)
{
Root = Math::Sqrt(k);
Info = " ";
Info = String::Concat(Info, k.ToString());
Info = String::Concat(Info, "\t ");
Info = String::Concat(Info, Root.ToString());
RootsListBox->Items->Add(Info);
}
}
|
How does this code work? The Info variable is used to hold a string that we build up
to hold the data that we want to display on an individual line of the ListBox. To be more precise,
it is a pointer to this string as you can tell from the ^ in String^ Info .
Notice how we start by calling a Clear function on the Items collection
in RootsListBox . We are again being a bit loose with our terminology since
RootsListBox is really a pointer to the ListBox and RootsListBox->Items
is a pointer to the Items collection object. That is why we see the use of the
arrow notation instead of the dot notation to pick out a field or function. In any case,
the Clear function is used to wipe out old data whenever the user clicks on the button.
If the user clicks the button 2 or more times we do not want the output appended together. Rather,
we just want to see the output for the last set of square root calculations.
We next extract the Text from NumberBox and use the ToInt32
function to convert this string to an integer. We should really check to see if this conversion works
or fails, but we will delay the handling of exceptions like this to the
First Advanced Windows Forms Example.
We then use a FOR loop to go through the desired number of positive integers and insert a line of data
into the ListBox for each such integer.
The Math::Sqrt(k) function call obviously calculates the square root of integer k.
Initially we have our Info string
pointer set to point to a string of 3 blanks. Then we concatenate onto the end of this string
the result of converting the current integer to a string. Then we concatenate the literal string
"\t " . Note that the \t indicates a tab character. It and the blanks in this
literal string are used for spacing between the integer and its square root.
Finally we use Root.ToString() to convert our calculated square root to string form
and concatenate this onto the end of the Info string as well.
The Add function is then used to add the string we have build up, the one that
Info points to, to the Items collection in RootsListBox .
Note again the use of the arrow notation since both RootsListBox and
Items are really pointers to objects and not the objects themselves.
The Code: C# Version
In Design View for your Form1.cs, double click on the button on your form so as to produce an outline (prototype)
for your click handler. Modify it to look like this:
private void StartButton_Click(object sender, EventArgs e)
{
long k, Count;
double Root;
String Info;
RootsListBox.Items.Clear();
Count = Convert.ToInt32(NumberBox.Text);
for (k = 1; k <= Count; k++)
{
Root = Math.Sqrt(k);
Info = " ";
Info = Info + k.ToString();
Info = Info + "\t ";
Info = Info + Root.ToString();
RootsListBox.Items.Add(Info);
}
}
|
How does this code work? The Info variable is used to hold a string that we build up
to hold the data that we want to display on an individual line of the ListBox.
Notice how we start by calling a Clear function on the Items collection
in RootsListBox .
The Clear function is used to wipe out old data whenever the user clicks on the button.
If the user clicks the button 2 or more times we do not want the output appended together. Rather,
we just want to see the output for the last set of square root calculations.
We next extract the Text from NumberBox and use the ToInt32
function to convert this string to an integer. We should really check to see if this conversion works
or fails, but we keep things simple by skipping that exception handling here.
We then use a FOR loop to go through the desired number of positive integers and insert a line of data
into the ListBox for each such integer.
The Math.Sqrt(k) function call obviously calculates the square root of integer k.
Initially we have our Info string
set to a string of 3 blanks. Then we concatenate onto the end of this string
the result of converting the current integer to a string. Then we concatenate the literal string
"\t " . Note that the \t indicates a tab character. It and the blanks in this
literal string are used for spacing between the integer and its square root.
Finally we use Root.ToString() to convert our calculated square root to string form
and concatenate this onto the end of the Info string as well.
Although you can use the following three lines to concatenate the appropriate strings in variable Info,
C# provides the + operator for concatenating strings. That is simpler to use, so the above code did use the +
to concatenate strings.
Info = String.Concat(Info, k.ToString());
Info = String.Concat(Info, "\t ");
Info = String.Concat(Info, Root.ToString());
|
After building up the Info string, the Add function is used to add the string
to the Items collection in RootsListBox . The user will thus be able to see it
in the list box.
Try It Out
Save all of your files, then build your new project. If there are syntax errors, fix them and build again.
Then run your program by using Debug, Start Without Debugging. Try it out and make
sure that it works correctly.
Back to the main page for Software Design Using C++
|