The For Next Loop and Text Box control can be used to create a great variety of different lists.
You have been contracted by Ace Taxi Company to produce a Visual Basic program that will make a list of charges based on distance traveled. This list will be displayed in a Text box. The current charges are $2.50 flag fall plus $1.25 per kilometre travelled. Your program will need to display the fare paid for distances from 1 to 100 kms.

This program requires the For .. Next structure for distances from 1 to 100. The commands within the block are repeated 100 times. As the block repeats, Kilometers goes from 1 to 100.
In VB6, as the output is larger than one screen, a text box with a the property Multiline set to true and Scrollbar set to vertical is required to display the information.
However, as mentioned before, VB.Net does not support this use of a Text box, so we need to use a ListBox instead.
It is important that constants are used for charge rate and flagg fall as these often change.
For Kilometers <- 1 To 100 do Set Charge To Kilometers * ChargeRate + Flagfall Write Kilometers and Charge Next Kilometers
Private Sub ProcessCmd_Click()
Const ChargeRate = 1.25
Const Flagfall = 2.50
Dim Kilometers As Integer
Dim Charge As Decimal
lstDisplay.Items.Add(" Kilometers " & " Charge"
For Kilometers = 1 To 100
Charge = Kilometers * ChargeRate + Flagfall
lstDisplay.Items.Add(" " & Kilometers & " $" & Charge)
Next Kilometers
End Sub
In the first lines of the program, the headings are put at the top of the List box. The charge is calculated and a message is added to the List box while Kilometers increases from 1 to 100. The end result is a list of 100 charges.
Now you will find that the data does not line up very well under the headings. It would be ideal to use tab stops here!! The VB.Net Help gives this piece of sample code to demonstrate how tab stops are used.
Private Sub CreateTabStopList()
Dim listBox1 As New ListBox()
listBox1.SetBounds(20, 20, 100, 100)
Dim x As Integer
For x = 1 To 19
'the controlChars.Tab is the Tab command within the list box
listBox1.Items.Add(("Item" & ControlChars.Tab & x.ToString()))
Next x
' Make the ListBox display tabs within each item.
listBox1.UseTabStops = True
Me.Controls.Add(listBox1)
End Sub
We do not need all of it, but the bits that have to go in our program are shown in red. The last 3 lines goes after our loop the same as after the loop above. Don't forget to change the name of the listBox.
The ControlChars.Tab goes in twice - once between the headings, then again within the loop.
Author Mike Leishman
Last Updated 28 June 1998 Edited for .Net framework by FDS 18/2/07