Software Design Using C++Second Advanced Windows Forms ExampleThe PlanWe wish to improve upon the Fourth Intermediate Windows Forms Example, the one that displayed in a ListBox the square roots of the first so many positive integers. Our first improvement is to use a new control, a ListView, that gives us a nicer view of the data. Notice how it gives us column headings so that we can label the column for the original numbers and the column for the square roots. However, the square root data is formatted inconsistently. Some of them have many decimal places, whereas roots that are whole numbers (such as the square root of 4, which is 2) are shown with no decimal point and no decimal places. It would be nice to display all of the roots in a consistent manner as shown in this picture. That will be our other improvement. Getting StartedIf you still have your Fourth Intermediate Windows Forms Example project, create a new C++ Windos Forms App project (perhaps named Roots2). Then copy the controls from the old form to the new one, and copy the code as well. Refer to the copying directions in this previous example if need be. If you do not have your old project for the Fourth Intermediate Windows Forms Example you could recreate it. Adjusting the ControlsClick on the ListBox control and then press the Delete key. That's a quick way to get rid of that old ListBox. That will mean, of course, that much of old code no longer makes sense, but we will take care of that later. Then drag a ListView control from the Toolbox and place it where the ListBox had been. Resize it to be about the same size as the control just removed. Make sure that the ListView control is selected and go to the Properties Window. Change the name to RootListView. Change the BackColor (background color) to a light yellow. If necessary, click the + sign in front of the Font property so that you can see all of the items listed within it. Set Bold to True. That's the only change to the Font property, so you can click on the - sign and collapse that stuff to a single line. Next, change the ForeColor to a bright blue. For the app in the pictures above, the ActiveCaption color found under System was used. Make sure that the GridLines property is set to True. That puts in vertical lines to separate columns and horizontal lines to separate the rows. Make sure that the Scrollable property is True, though it should be so by default. We want the user to be able to scroll through the list of roots. The View property should be set to Details (and not to SmallIcon or one of the other choices). Save all of your files. Also right click on the new project in Solution Explorer and set this project to be the startup project. Code for the First ImprovementOpen your Form1.cpp file and change the code for your click handler function so that it looks like this:
Notice that we set up
However, before we get to the details of that, we first clear any old data out of
The structure of the loop is similar to that of our old application. However, once we have a new value
of At this point it would be best to save all of your files. Then build and run your application to see that it works reasonably. The running app should look like the one shown in this picture. Code for the Second ImprovementHere, we want to format those square roots consistently. To be able to do this we first need to make a few small additions to Form1.h. Open your Form1.h file in Code View. The prototype for the click handler function at the bottom of the file should be fine as is. At the top of the file put in these two additions (shown here with the comment "added for NumberFormatInfo"). No other changes or additions should be needed.
Then, in Form1.cpp revise the code for our click handler as follows:
It probably seems strange to see the creation of a
The next thing done in the above code is to set 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++ |