Now I think it is about time for loops! Loops are very important in programming. Loops tell a program to execute specific instructions in coding a certain amount of times. There are three types of loops used in Visual Basic, which are listed below, along with their coding structure:
Loops |
||
For Next loop: Structure: For [Condition]
|
DoWhile loop:
Structure: DoWhile [Condition]
|
DoUntil loop: Structure: Do [Condition]
|
If you have difficulty understanding the structures above, don't worry. I will explain line-by-line as we develop the program. Also, this is a longer section in the tutorial. Be sure you have the patience and attention span to take in all this information. Let's begin.
Create the form as seen above.
Name the form frmLoops
Name the project Loops or whatever you want.
Name the command buttons appropriately as follows:
cmdNextLoop
cmdWhileLoop
cmdUntilLoop
Add two more forms to the project.
Review: Project > Add Form (Do this
twice)
Name them as follows:
frmDoUntil
frmForNext
Set each form's BorderStyle properties to 1-Fixed Single and MinButton properties to True.
Open the code window for the form frmLoops.
Press tab, then type the following code:
Const conPassword As String = "Forgot"
Dim strGuess as String
Do While strGuess <> conPassword
strGuess = InputBox("Enter password:", "DoWhile
Loop")
Loop
MsgBox "Correct!", , "Good"
frmLoops.Top = (Screen.Height - frmLoops.Height) / 2
frmLoops.Left = (Screen.Width - frmLoops.Width) / 2
To explain this code:
Line 1: Declares constant conPassword as
a String and stores Forgot within it.
Line 2: Declares string variable strGuess.
Line 3: Begins Do While loop; Says Do below(strGuess =
InputBox("Enter password:", "DoWhile Loop")) While
strGuess <>(greater than or less than; not equal to) conPassword(Forgot).
It will execute Line 3 below if "Forgot" is not entered into
the Input Box .
Line 3: Shows user the input box.
Line 4: Integrates loop and indicates the end of the loop statements.
Line 5: Displays message box once the loop ends (if strGuess=Password).
Lines 6 and 7: Centers form on the screen.
Now let's code the DoWhile command button (cmdWhileLoop).
Open the code window for cmdWhileLoop.
Press tab, then type the following line in the sub-procedure for the command button:
Call Form_Load 'Calls the Form_Load
sub-procedure, which executes the code within it
Run the application and test. Notice that the Input Box exits when the password is correct, and does not exit when it is wrong, just as the code says
End the application and open the form frmUntil by double-clicking the form in the Project Explorer window.
Set the form's caption to DoUntil.
Double-click the command button tool in the toolbox to add a command button to the form.
Set the button's caption to Start and name the button cmdStart.
Double-click the form to open the form's code window.
Press tab, then type the following line in the
form's Load event procedure:
sngClicks = 0 'Sets the variable sngClicks to zero
when the form loads.
Select (General) in the code window's object box.
Type the following line in the code window:
Dim sngClicks as Single 'Declares a form-level variable sngClicks
as the Single data-type.
Go to the sub-procedure for the cmdStart button by selecting the object in the code windows object box.
Press tab, then type the following lines
Do 'Begins the DoUntil loop
sngClicks = sngClicks + 1 'Increments the variable sngClicks
by 1
Loop Until sngClicks = 5000 'Increments the variable sngClicks
until it reaches 5000; ends loop procedure
lblClicks(1).Caption = sngClicks 'Displays the value of the variable sngClicks
in the label lblClicks(1)
Open the form frmLoops.
Open the code window for the object cmdUntilLoop.
Press tab, then type the following line in the code window:
frmDoUntil.Show 'Shows the form frmDoUntil
If you run the application and display the form with the DoUntil loop, you notice that the variable Clicks is incremented very quickly. Increase the number to 500,000 (in the Loop Until code line; Ex: Loop Until sngClicks = 500000) and it will take a bit longer. Maybe try 50,000,000? If you put in a very large number, it may hang the machine, or take longer than you want to wait. Just press Ctrl + Break, then press the stop button to exit the application.
You can also use the DoUntil loop to repeat the password example in the DoWhile loop earlier.
Now let's do the For Next loop.
Enter the code below in the sub-procedure form cmdNextLoop.
frmForNext.Show
Recall that this simply loads the form frmForNext and displays it to the user.
Now let's set up the form frmForNext.
Open the form frmForNext.
Set the form's caption to For Next Loop.
Add the objects below and name them accordingly:
cmdCalc
cmdClear
chkItem(0)
chkItem(1)
chkItem(2)
txtQty(0)
txtQty(1)
txtQty(2)
lblTotal
lblTotalValue
Notice the access keys in the command buttons. The label lblTotal is the label displaying the text Total quantity:. The label lblTotalValue is the label to the right of it, which may be difficult to see in the image below.
Let's do the easy part first; coding the cmdClear button.
Enter the code below in the code window for the object cmdClear:
For intItem = 0 To 2
txtQty(intItem).Text = ""
chkItem(intItem).Value = Unchecked
Next intItem
To explain this code, see below:
Line 1: Begins loop; takes intItem and sets the
range of values from 0 to 2.
Line 2: Clears the text boxes txtQty; it sets the Text
property to "" (empty). Since intItem has the values 0,
1, and 2, the intItem in txtQty(intItem) is set to 0,
1, and 2. It clears all the text boxes with values 0, 1,
and 2, which covers each text box on the form. Remember as explained in
line 1, line 1 sets these values.
Line 3: Same as line 2, but with the check boxes. Unchecks all check
boxes with intItem (standing in for object's Index property) equaling 0,
1, and 2.
Line 4: Integrates loop; tells the loop to go to 1 when it
finishes 0. Remember: The range is 0 to 2.Therefore, the
loop stops once intItem = 2, which is true in the last check box
and text box
Try running the application.
Enter password at prompt to access form (Forgot; be sure to match the case).
Click the command button that displays the form we just completed (caption on button reads: "ForNext").
Check all the check boxes, then enter some numbers into the text boxes.
Click the Clear button, which clears the text entered and the checks in the check boxes.
Now let's get this thing to display the quantities.
Set each textbox's enabled property to false in the properties list.
If the text box is enabled, the loop always looks at the value. This way, the text box cannot respond.
Open the code window and access the chkItem event procedure.
Press tab, then type the following lines below:
For intItem = 0 To 2
If chkItem(intItem).Value = Checked Then
txtQty(intItem).Enabled = True
Else
txtQty(intItem).Enabled = False
txtQty(intItem).Text = ""
End If
Next intItem
Line 1: Begins loop; takes intItem and sets the
range of values from 0 to 2.
Line 2: Checks to see which check boxes are checked; checks intItem
from 0 to 2.
Line 3: Enables the quantity text box corresponding with the index for chkItem.
Line 4: Otherwise, do the following.
Line 5: Disables the quantity text box according to the index of the
check box.
Line 6: Clears the text in the text box according to its index
Line 7: Ends If statement.
Line 8: Integrates loop; goes to next values of intItem and
continues to go through the values; 1 then 2.
Now let's get this program working.
Open the code window for the command button cmdCalc.
Type the following lines:
Dim intSubtotal As Integer
The line above simply declares a local variable called intSubtotal as an integer in the cmdCalc sub-procedure.
Type the following line in the General Declarations section of the code window:
Dim intItem as Integer
The line above simply declares a form level variable as an integer. We will use this for our loop values.
Now let's finish this sub-procedure. Type the following lines below your variable declaration (Dim intSubtotal As Integer):
For intItem = 0 To 2
If chkItem(intItem).Value = Unchecked Then lblTotalValue.Caption = intSubtotal
If chkItem(intItem).Value = Checked Then
intSubtotal = intSubtotal + Val(txtQty(intItem).Text)
lblTotalValue.Caption = intSubtotal
End If
Next intItem
Here is the explanation for this code:
Line 1: Begins the For loop, using value range
of 0 to 2.
Line 2: Checks to see if all the checkboxes are unchecked. If they are,
it displays 0, because the variable intSubtotal will equal zero
under this condition. This line is not necessary, but if this line is not
typed in, the label lblTotalValue will not show zero when
nothing is checked.
Line 3: Checks to see which checkbox is checked.
Line 4: Adds the value of the corresponding quantity text box to the
variable intSubtotal. This is needed in the loop so the value in the
quantity text box has a place to go.
Line 5: Displays the value of the variable intSubtotal in the
label lblTotalValue.
Line 6: Ends the If statement.
Line 7: Increments the For Next loop; goes to the next
value of intItem.
I hope you enjoyed this rather long tutorial. Now that you have learned about loops, things are going to soon become much more interesting and complicated.
Now you can move onto Page 14 if you still have the energy.