CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++



Second Intermediate Windows Forms Example



The Plan


The 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 Example


Since 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 InitializeComponent function and the #pragma endregion but before the two closing right braces (the first of which has a semicolon after it, marking the end of the Form1 class declaration). At this point the end of your new Form1.h should look something like this:


void InitializeComponent(void)...
#pragma endregion

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

    Length = Convert::ToDouble(LengthBox->Text);
    Width = Convert::ToDouble(WidthBox->Text);
    Area = Length * Width;
    AreaBox->Text = Convert::ToString(Area);
    }

};
}

Note that you can click on the + sign in front of the InitializeComponent function to see all of the code inside. Normally it is best to leave this section collapsed into one line as this is code that was automatically generated and you will not change it. Save all of your files before going on.

Adding Simple Error Checking


Since 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:


private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
    {	
    Double Area, Length, Width;

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

You can see how we put the "Area Undefined" message into AreaBox if either Length or Width (or both) are not positive. Remember that the double ampersand is the C++ notation for Boolean AND. Thus only if both Length and Width are positive do we calculate and display the area. Note, too, that we have used one of each of the ways previously shown to convert a string to a double. This is just to illustrate the possibilities for you.

Placing Code in Form1.cpp


As 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:


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

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

Note that only the first line has been modified. The private: modifier has been removed and the class name Form1:: has been placed in front of the CalcButton_Click function name. The latter alerts the compiler that this function belongs to the Form1 class (which is set up in Form1.h).

Then, in the Form1.h file for this new project, change the end of the file to the following:


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

Note that we have deleted the code for the function as well as the braces that contained the code. The modifier private: could be omitted here since our function would still be within the private section set up for the previous function in the Form1 class. However, it does not hurt to repeat the word private. The one new item is the semicolon at the end of the line for CalcButton_Click. This semicolon is important. It must always be used at the end of a function prototype.

Try It Out


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

Author: Br. David Carlson with contributions by Br. Isidore Minerd
Last updated: November 21, 2014
Disclaimer