Visual Basic Programming

Page 7

Now I will teach you how to use another math function. Specifically, the Mod function and integer division (\).

I put an "X" in these labels so that we can see the labels when we are using the development environment.

Let's write some code. We'll start with the boring stuff to get it over with.

As you can see, this code clears the captions of the two label that display the results and the text in the text boxes on the form.

**We have been using the End keyword so far to close our applications. Now I will show you another way to do so.

This code simply closes the program. The difference here though is that the keyword End closes an entire application. Using End closes all forms and processes which are part of the program. By using the Close() method, only the specified object is unloaded (taken out of the computer's memory).

Because we are working with a form, this form is treated as a class. We cannot reference the form name directly. The keyword Me is a way to address the super-class. A super-class is simply the main class that all other objects are part of. The objects on the form are also classes. The super-class is the class that encompasses all the objects. In Visual Basic, we cannot address the super-class by its name. We need to use the keyword Me instead. So in this case, Me is simply the form frmOranges.

Now lets write the code for the command button cmdCalculate. We will have this program put 12 oranges in each box.

Now lets understand this code. The lblBoxesQuantity.Text = Val(txtOranges.Text) \ 12 code divides the value of the text in txtOranges by 12 without a decimal. The reason for no decimal is because you used the integer division (\) sign, as opposed to the division sign(/). An integer is any non-decimal number (whole number), which includes negatives, positives and zero. Therefore, this operation does not return decimals. The next line of code, lblRemainingQuantity.Text = Val(txtOranges.Text) Mod 12, places the remaining oranges in the lblRemainingQuantity caption. The Mod function tells the program to divide it by a number (in this case 12), and to return (display or compute) the remainder of the division only.

**Save this project AND form as something. I will teach you how to do Rollovers on Page 10 using your same interface.**

Having fun yet? Hope you understood this lesson. Now its time to advance to Page 8.

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