Software Design Using C++Third Advanced Windows Forms ExampleNote that this functionality is no longer available. The GoalThe goal is to create the app shown in this picture. The idea is that the user enters a list of temperature values in the text box on the left and then clicks one of the three buttons in the middle. The first button causes the app to compute and display the average of the temperatures. The next button causes the app to show the average (in the small text box as for the previous button) and also those temperatures which are above the average (in the large text box on the right). The last button is similar as it shows the average and also the list of temperatures which are below the average. Our picture shows the application after the user has entered a list of temperatures and clicked the "Find Above Avg" button. Getting StartedSince you have created a number of Windows forms apps by now, complete directions will not be given. Instead the main steps will be outlined, with new items looked at in more detail. Of course, you also know that you can often do things in other ways, select different colors, etc. Create a new Windows forms app and place on it the labels, buttons, and 3 text boxes as shown in our picture of the running application. Change the text field of the form to Temperatures. Name the text box on the left as OriginalTextBox, the small text box as AvgTextBox, and the large text box on the right as ResultsTextBox. Let's adjust some properties of the overall form. You probably want to change the FormBorderStyle to Fixed3D so that the edges cannot be dragged by the user. It makes little sense to allow the user to resize this form. Also change the MaximizeBox property to False so that the user is not presented with a maximize button in the upper right corner of the app. How about properties for AvgTextBox? Set ReadOnly to True since the user should not be able to manually change the value of the average. That's about all that you need to do here. In OriginalTextBox set Multiline to True so that the box can contain many lines of data. Change the size of this text box so that it can display several lines of data at once. For the Scrollbars property select Vertical so that a vertical scroll bar will be available for the user to scroll through the items in the box. In ResultsTextBox set ReadOnly to True and Multiline to True. Adjust the size of the box in a way similar to what you did with OriginalTextBox. For the Scrollbars property select Vertical. For the topmost of the 3 buttons, change its name to AverageButton and the displayed text to "Find Average". For the middle button, change its name to AboveAvgButton and the text to "Find Above Avg". For the last button, change its name to BelowAvgButton and the text to "Find Below Avg". You can adjust the TabIndex and TabStop properties of the text boxes and buttons if you wish. Save all of your files. Also right click on the new project in Solution Explorer and set this project to be the startup project. Beginning on the CodingBegin by adding the 2 lines marked below to the top of your Form1.h file. These changes are primarily to allow use to use NumberFormatInfo
(as we have elsewhere) to control the formatting of numbers.
Double click on each of the 3 buttons so as to generate the outline of a click handler function for each. These will be located in Form1.h, but we will want the code for these functions in Form1.cpp. Thus, in Form1.h change the click handler outlines to simply be function prototypes as shown below:
First Click HandlerIn Form1.cpp fill in the complete click handler for the first button so that it matches the following:
Essentially the above code loops through all of the temperature numbers in Note that this click handler begins by clearing the text boxes used for the answers (the average and the list of temperature above or below average). We don't want old results still showing up! The click handler also uses the method shown in the previous example to format our numbers. Here we use 2 decimal places.
It is suggested that you look up Save all of your files before going on. Second Click HandlerStill in Form1.cpp, fill in the complete click handler for the second button. Make it match the following:
The above function begins by calling the first click handler. This may be unnecessary, but the
user might click the second button without first clicking the first button to find the average.
Thus it is safer to do things this way. You could perhaps instead try to get a number value out
of
The number formatting is the same kind of thing that we used before, though here we do not worry
about the number of decimal places. What is different is that we have used an ordinary
Save all of your files before going on. Third Click HandlerIn Form1.cpp fill in the complete click handler for the third button. Use the following code:
Other than the fact that this code looks for temperatures that are below, not above, the average the code is the same as that used in the second click handler. No additional explanation, therefore, should be needed. At this point 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 our picture. Back to the main page for Software Design Using C++ |