Software Design Using C++Fourth Intermediate Windows Forms ExampleThe GoalWe 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 StartedCreate a new 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 ControlsFrom 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 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 CodeIn Design View, double click on the button on your form so as to produce an outline (prototype) for your click handler. In Form1.h it should look like this:
Then, in Form1.cpp place the code for this new function. Use the following:
How does this code work? The
We next extract the
We then use a FOR loop to go through the first so many positive integers and insert a line of data
into the ListBox for each such integer. Note that we use long integers since the user might want to
find a huge number of square roots. The
The string manipulations may be new to you. The
The Try It OutSave 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++ |