Software Design Using C++Second Intermediate Windows Forms ExampleThe PlanThe idea here is to make a Windows Forms App very similar to the first one we did on calculating the area of a rectangle. However, we will make small improvements. Specifically, we will put the code for the click handler into the Form1.cpp file and add checks to handle the case when either the length or the width are not positive. Otherwise the two apps are identical. If you are using C# instead of C++, try this version instead. It simply adds error-checking to the first Windows Forms App example. Copying from the First ExampleSince so much is the same, we do not want to waste time recreating what we already did in the first intermediate example. Open that example in Visual Studio. Then create a new C++ Windows Forms App, perhaps called Area2. Add it to the same solution as the first example. Use Solution Explorer to bring up in design view the Form1.h file from the first example. Drag the mouse from the upper left of the form to the lower right so as to "rubber-band" the controls on the form. Avoid the title bar. While these controls are selected do Edit, Copy. Then go to Form1.h for the new example and use Edit, Paste to put the copied controls onto the empty form.
Next, go to the code for Form1.h in the first example. Copy the comments and code for the click
handler function. Then paste it into the corresponding location in the new Form1.h file. Be sure that
the code gets pasted in after the
Note that you can click on the + sign in front of the Adding Simple Error CheckingSince it does not make sense to find the area if the length or width is given as a negative number, we should check for this. Even a length of width of zero does not really give a sensible area, so we will also check for that. Thus we wish to only calculate the area if the length and width are positive. In any other case we will put an error message in the AreaBox .
To do this, change the code for our click handler to look like the following:
You can see how we put the "Area Undefined" message into Placing Code in Form1.cppAs shown in the Objects and Classes section of these C++ web pages, it is fairly common to only put the prototype of a function inside the class declaration (in Form1.h in our case) and to place the code for the function in a .cpp file (Form1.cpp in our case or Area2.cpp or whatever you named it). Let's proceed to do that. Copy the comments and code for our click handler function and place them at the very end of the Form1.cpp file (which you can open by double clicking on the desired file in Solution Explorer). Then modify this code in Form1.cpp to look like this:
Note that only the first line has been modified. The Then, in the Form1.h file for this new project, change the end of the file to the following:
Note that we have deleted the code for the function as well as the braces that contained the code.
The modifier Try It OutSave all of your files. If this new project is in the same solution as the previous project (or any other project), then you need to indicate that the new project is the one that you wish to run. (Note that as long as you are editing files for the new project, the Build menu should show an option to build that particular project, so you will probably have little trouble with building. You have to be a little more careful with what project gets executed, the so-call "startup project".) In Solution Explorer, right click on the new project and select "Set as Startup Project". The line for the project will then show in bold in Solution Explorer. Now when you use Debug, Start Without Debugging, it will be this project which is run. If you are not careful about what project is selected as the startup project, you may execute an older project and not realize that you have failed to run the new project. Build your new project. If there are syntax errors, fix them and build again. Then run your program by using Debug, Start Without Debugging. Make sure that it works reasonably both when given positive length and width as well as 0 or negative values. If you wish you can also run your program by finding the executable file in My Computer and double clicking on it. It will probably be inside a Debug folder that is within your solution folder. The filename would be Area2.exe or something similar (consisting of the project name with the .exe extension added). Back to the main page for Software Design Using C++ |