Visual Basic Programming

Page 14

In this tutorial, I created more of an exercise more than new material. You will create the program above, which I am taking you through. This exercise is a test of you ability to understand a few math functions, particularly with circles. It also includes a simple loop. Once again, this tutorial is a bit long, so be sure you have the patience today. Let's begin.

Notice that all the option buttons are sitting on a rectangle. That object is a frame, which is used to group related objects together.

Recall that the number in parenthesis is set by assigning a number to the index property.

Now let's code this application.

Line 1: Sets the caption of the label lblEntry to Enter Circumference.
Line 2: Clears the text box txtEntry.
Line 3: Clears the label lblResult.
Line 4: Sets the value of the option button optFuntion(0) to true.

Note on option buttons:

You may already know that option buttons are the other form of checkboxes. However, only one option button in its group can be selected at a time. The value property is only True or False.

Now we will code the command buttons.

Notice that the first line reads Private Sub optFunction_Click(Index As Integer). This indicates that the object is indexed and has multiple parts. Since they are indexed, this sub-procedure encompasses all the option buttons. The code will directly look at which option button is selected.

Now we will get this thing working.

Line 1: Looks at indexes 0, 1, 2, and 3.
Line 2: Checks to see which option button is checked. Sets the label caption of lblFunction to whatever the caption is of the selected option button, then puts a colon at the end.
Line 3: Integrates loop; goes to next value of i (1 then 2 then 3).

The line above simply creates a constant variable named conPi as the data-type Single and stores the value 3.14 in it. Constants store data in a container (conPi in this case) just like a variable, but the value never changes. Thus we have the name constant.

Now we will set up some functions.

A function is simply a set of commands that executes when it is called. Functions are usually designed to perform a certain task. While sub-procedures are the same as functions in many ways, they still differ. A function is called in coding, while a sub-procedure is usually executed by an object's event, such as click or LostFocus. A sub-procedure can take the place of a function if it is called, and vise-versa.

Line 1: Creates a private function called Radius
Line 2: Divides the value of the text in text box txtEntry by Pi, then divides the entire result by two.
Line 3: Ends the function.

The function above calculates the radius. The user will be asked for the circumference. The formula for circumference is Pi * Diameter, so we reverse this by dividing Pi. That gives us the diameter. The radius is half the diameter, so we then divide the result by 2.

Line 1: Creates a private function called Diameter.
Line 2: Divides the value of the text in text box txtEntry by Pi.
Line 3
: Ends the function.

The function above calculates the diameter. The user will be asked for the circumference. Pi * Diameter gives us the circle's circumference, so dividing the circumference by Pi reverses the multiplication, thus giving us the circles diameter.

Line 1: Creates a private function called Area.
Line 2: Squares the value of the text in the text box txtEntry, then multiplies it by Pi. (Pi R Squared: Formula for circle area)
Line 3: Ends the functions 

The function above calculates the area. The user will be asked for the radius. Pi * Radius^2 (Radius squared) is the formula for the area of a circle.

Line 1: Creates a private function called Circumference.
Line 2: Multiplies the value of the text in the text box txtEntry by Pi.
Line 3
: Ends the functions 

The function above calculates the circumference. The user is asked for the diameter. Pi * Diameter gives us the circle's circumference.

Now let's code the cmdCalc sub-procedure.

Lines 1-4: Depending on the option button selected, it will call (execute) a specific function that we constructed above.
Line 5: Rounds the result in lblResult to 3 decimals.

The keyword Round is another Visual Basic function, just like Val or Mod. It is more precise than integer division, which rounds a number to the nearest whole number. The number you specify after the object is the amount of decimal places the calculation will round the result to using the Round function.

I hope that your understanding of loops has increased a bit, as well as your mathematic abilities. It certainly will as we go through this tutorial more, because they will appear more frequently.

Now it is time to move onto Page 15.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30