Software Design Using C++First Advanced Windows Forms ExampleOur GoalDon't be too worried about the word advanced in the title as this example will not be horribly advanced! However, it does build on previous work, so be sure that you have worked through the second intermediate example. We begin with the area calculation application created in that example and then add exception handling to it. That means that you should also be somewhat familiar with exception handling before trying this new example. It is suggested that you read the exception handling section of these web pages before going on. Copying from the Second Intermediate ExampleCreate a new C++ Windows Forms App project in the same solution as the Second Intermediate Example. Name this project Area4 or something similar. We then wish to copy items from the second intermediate project to our new project. As we did in copying from the first to the second project, copy both the controls and the code to the appropriate spots in the new project. The controls are on Form1.h in design view and the code that we wrote is found in both Form1.h (in code view) and Form1.cpp. 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 thus far. At this point the end of your Form1.h file should look like the following. Note that what we have is a function prototype for the click handler.
Also, the code for this function (found at the end of your Form1.cpp file) should look like this:
Exception HandlingWe already noted in earlier versions of this area calculation app that it would crash if the user supplied bad data, such as abc instead of a proper number for the length or width. In fact, if the user fails to supply any input for either length or width, then the program crashes.
Change the code in Form1.cpp for the click handler so that it catches any exception. If an exception
occurs, we simply put a message to that effect into
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. Make sure that it works correctly, especially in cases where a valid number is not put into the length or width box. Back to the main page for Software Design Using C++ |