Visual Basic | Variables and constants exercise | Get the Add button to work in a basic calculator

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

Software ==> Visual Basic  (46 exercises)
Version ==> Any version of VB
Topic ==> Variables and constants  (3 exercises)
Level ==> Average difficulty
Subject ==> Visual Basic training
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

To avoid having to draw the form needed by this exercise, right-click on the name of your project in Solution Explorer then choose Add --> Existing Item... (you can also press CTRL + D to do the same thing).

Choose only the file called frmCalculator.cs in the above folder to import it into your project, then make this the default form.  When you run your application, you should now see a simple calculator form.

Attach code to the Calculate button.  Begin with a Try ... Catch clause to check that you can read the two numbers entered into integer variables:

Error message if not integers

Your error-trapping should exit the subroutine if a user has entered anything but whole numbers into either of the two input boxes.


If on the other hand you have a sensible user, he or she should see the sum of the two integers entered:

Message box displaying sum

What you should see if you enter genuine numbers in the two boxes; the correct answer at the bottom of the form.


Close down any forms that you have left open.

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post