Now I will teach you how to use another math function. Specifically, the Mod function and integer division (\).
Recall:
Add the controls shown above and name them appropriately. Set the access keys for the command buttons as seen above.
Name the command buttons like so going down the row.
cmdCalculate
cmdPrint
cmdClear
cmdClose
Name the labels like so going down the row.
lblName
lblOranges
lblBoxes
lblRemaining
Now name the labels to the right of them.
lblBoxesQuantity
lblRemainingQuantity
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.
Double-click the command button cmdClear.
Type the following lines:
txtName.Text = ""
txtOranges.Text = ""
lblBoxesQuantity.Text = ""
lblRemainingQuantity.Text = ""
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.
Double- click the cmdCalculate command button and enter the following lines into coding:
lblBoxesQuantity.Text = Val(txtOranges.Text) \ 12
lblRemainingQuantity.Text = Val(txtOranges.Text) Mod 12
Lock the controls on the form.
Recall:
Format > Lock Controls.
OR
Right-click on the form and select Lock Controls.
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.