CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++



First Advanced Windows Forms Example



Our Goal


Don'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 Example


Create 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.


/* The following function fires when the Calculate button is clicked.
   It takes the values in the LengthBox and WidthBox, converts them to numbers,
   calulates the area, and displays the area in the AreaBox.
*/
private: System::Void CalcButton_Click(System::Object * sender, System::EventArgs * e);

Also, the code for this function (found at the end of your Form1.cpp file) should look like this:


/* The following function fires when the Calculate button is clicked.
   It takes the values in the LengthBox and WidthBox, converts them to numbers,
   calulates the area, and displays the area in the AreaBox.
*/
System::Void Form1::CalcButton_Click(System::Object * sender, System::EventArgs * e)
   {	
   Double Area, Length, Width;

   Length = (LengthBox->Text)->ToDouble(NULL);
   Width = Convert::ToDouble(WidthBox->Text);
      if ((Length > 0) && (Width > 0))
      {
      Area = Length * Width;
      AreaBox->Text = Area.ToString();
      }
   else
      AreaBox->Text = "Area Undefined";
}

Exception Handling


We 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 AreaBox, the place where we normally put the answer to the calculation. The most likely reason to have an exception is that what is in LengthBox or WidthBox cannot be converted to a number (of type double). Here is how your revised code should look:


System::Void Form1::CalcButton_Click(System::Object * sender, System::EventArgs * e)
   {
   Double Area, Length, Width;

   try
      {
      Length = (LengthBox->Text)->ToDouble(NULL);
      Width = Convert::ToDouble(WidthBox->Text);
      if ((Length > 0) && (Width > 0))
         {
         Area = Length * Width;
         AreaBox->Text = Area.ToString();
         }
      else
         AreaBox->Text = "Area Undefined";
      }
   catch (...)
      {
      AreaBox->Text = "Exception Occurred";
      }
   }

Try It Out


Save 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++

Author: Br. David Carlson with contributions by Br. Isidore Minerd
Last updated: August 27, 2009
Disclaimer