18.01.2013 Views

Mastering Visual Basic .NET

Mastering Visual Basic .NET

Mastering Visual Basic .NET

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The Single data type can hold much larger values. Besides, the Single data type can also hold noninteger<br />

values. I’m assuming you won’t ask for a loan of $25,000 and some cents, but if you want to<br />

calculate the precise monthly payment for a debt you have accumulated, then you should be able to<br />

specify a non-integer amount. In short, we should have declared the LoanAmount variable with the<br />

Single data type in the first place (but then I wouldn’t have been able to demonstrate the overflow<br />

exception).<br />

An overflow error can’t be caught with data-validation code. There’s always a chance your calculations<br />

will produce overflows or other types of math errors. Data validation isn’t going to help here;<br />

you just don’t know the result before you carry out the calculations. We need something called error<br />

handling, or error trapping. This is additional code than can handle errors after they occur. In effect, you’re<br />

telling VB that it shouldn’t stop with an error message. This would be embarrassing for you and<br />

wouldn’t help the user one bit. Instead, VB should detect the error and execute the proper statements<br />

that will handle the error. Obviously, you must supply these statements, and you’ll see examples of<br />

handling errors at runtime in the following section.<br />

Building a Math Calculator<br />

Our next application is more advanced, but not as advanced as it looks. It’s a math calculator with a<br />

typical visual interface that demonstrates how <strong>Visual</strong> <strong>Basic</strong> can simplify the programming of fairly<br />

advanced operations. If you haven’t tried it, you may think that writing an application such as this<br />

one is way too complicated, but it isn’t. The MathCalculator application is shown in Figure 2.6, and<br />

you’ll find it in this chapter’s folder on the CD. The application emulates the operation of a handheld<br />

calculator and implements the basic arithmetic operations. It has the structure of a math calculator,<br />

and you can easily expand it by adding more features. In fact, adding features like cosines and<br />

logarithms is actually simpler than performing the basic arithmetic operations.<br />

Figure 2.6<br />

The Calculator<br />

application window<br />

BUILDING A MATH CALCULATOR<br />

Designing the User Interface<br />

The application’s interface is straightforward, but it takes quite a bit of effort. You must align buttons<br />

on the form and make the calculator look as much like a hand-held calculator as possible. Start<br />

a new project, the MathCalculator project, and name its main form CalculatorForm.<br />

Designing the interface of the application isn’t trivial, because it’s made up of many buttons, all<br />

perfectly aligned on the form. To simplify the design, follow these steps:<br />

1. Select a font that you like for the form. All the Command buttons you’ll place on the form will<br />

inherit this font. The MathCalculator application on the CD uses 10-point Verdana font.<br />

45

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!