using math in vb - Lakeview Christian Academy

advertisement
Math Expressions
The computer will calculate mathematical expressions, that's what it's best at. Here is a list of some of the
mathematical expressions:
+ Addition
- Subtraction
* Multiplication
/ Division
\ Integer Division (chops off the decimals of the answer)
^ Exponents
Take special note that if you divide and want decimals, you need a / (forward slash). If you are doing integer
division and want to chop off the decimals you need to use a \ (back slash).
Visual basic has a special class called the Math class. There are many functions that this class can do. It is used
by first typing in Math followed by a . (period). When you type in the period, the computer should show the
options available for this class. This is called intelli-sense.
There are more mathematical functions which perform a task and return a value. Here is a list of some of the
common math functions: (Remember to put the Math. before the function. This calls the Math class.)
Math.Sqrt(number) returns the Square Root of number
Math.Abs(number) returns the absolute value of number
Math.Round(number, places)returns a Double representing a value rounded to a specified number of decimal
places.
Math.Max(number1, number2) returns the number which is larger
Math.Min(number1, number2) returns the number which is smaller
All the same rules apply to math in computer programming as in math class. There are rules of precedence
which operation is done first. You can use parentheses to group operations together. You use the same math
equations in a program as you do in math class.
Remember: P(lease) E(xuse) M(y) (D)ear (A)unt (S)ally
Example
Write a program that calculates the math equation chosen by the user, with numbers provided by the user. Set
up a Visual Basic Project with the following objects and properties. Note that it is not necessary to have a
"Calculate" button. The click on a radio button is the event that will run the code. This event is called
"CheckedChanged"; earlier, you've used clicking on a button, an event called "Click".
Object
Form
Properties
Name: frmCalculations
Text: Calculations
Your form should look similar to this:
Label
Name: lblMath
Text: Enter two numbers and
choose the operation
Text Box
Name: txt1
Text: blank
Text Box
Name: txt2
Text: blank
GroupBox
Name: grpCalc
Text: Select a math operation
RadioButton
Name: rdoAddition
Text: Addition (+)
RadioButton
Name: rdoSubtraction
Text: Subtraction (-)
RadioButton
Name: rdoMultiplication
Text: Multiplication (*)
RadioButton
Name: rdoDivision
Text: Division (/)
RadioButton
Name: rdoIntDivision
Text: Integer Division (\)
Label
Name: lblResult
Text: blank
Button
Name: btnReset
Text: Reset
Button
Name: btnEnd
Text: End
Now add the code for the appropriate events in your project. Double-click on each radio button, which will
generate the "Private Sub" and "End Sub" lines for that event, and type the code into that space from the image
below. Each radio button needs its own sub with its own code. Make sure to type only the indented lines of
code, not the lines that begin with "Private Sub" and "End Sub", which are provided with all the proper
housekeeping details when you double-click on the object on the form.
Note that the code for each radio button is very similar to the others. In each case, when the event happens (the
user clicks on a radio button), the program must get the data (read from the text boxes), do the work (perform
the specific calculation for that radio button), and display the results (write data to the results label). It is quite
efficient to copy the code, once you have it working for one radio button, into the location for another radio
button, and change the math formula for the new radio button.
If finish:
The PictureBox Control
Images can make an application more interesting or improve usability. You can add a PictureBox from the
toolbox.
These are some of the PictureBox control properties:
Name It is good programming style to begin PictureBox object name with pic.
Image contains the button
that is clicked to display the Open dialog box where an image file can be selected.
Image files can be BMP, JPG, GIF, PNG, ICO, EMF, and WMF formats.
Sizemode can be set to either Normal, StretchImage, AutoSize or CenterImage. AutoSize is often the best
choice. Normal will often make it so you only see part of the picture.
Visible can be set to either True or False. Visible is often used at run time to display or hide an image. If the
image is set to False, the picture will not show when the program first runs.
Size is the picture box size in pixels.
Write a visibility program. The program should display an image on the form. When the Not Visible Button is
clicked, the image should disappear. When the Visible Button is clicked, the image should appear. You may use
a different graphic if you choose. Name your form frmVisible. The program should look similar to this:
Download