41. Using Step in For ...Next Loop - Visual Basic 2017

preview_player
Показать описание
Using Step - (See Description for Procedure)

You don’t have to start your loop at 1; you can pick any value you want. Nor do you have to increment
the control value by 1 on each iteration; again, you can increment by any value you want.

In this Try It Out, you learn about the flexibility of the Step keyword.

1. Return to the Forms Designer for the Loops project (Tutorial 40). Add a Button control to your form. Set its Name property to btnForNextLoopWithStep and its Text property to For Next Loop w/ Step.

2. Double-click the button and add the following bolded code in the Click event handler:

Private Sub btnForNextLoopWithStep_Click(sender As Object,
e As EventArgs) Handles btnForNextLoopWithStep.Click

'Clear the list
ClearList()
'Perform a loop
For intCount As Integer = 4 To 62 Step 7
'Add the item to the list
lstData.Items.Add(intCount.ToString)
Next

End Sub

3. Run the project and click the For Next Loop w/Step button.

Source: Beginning Visual Basic 2015 by Bryan Newsome