Lucky Lotto
Learn to make a Lucky Lotto example using Visual Basic 6 with this Tutorial that Celebrates the CESPage 25th Anniversary
This is a simple program, which will introduce you to Control Arrays, Standard Arrays, Label Control and Random Number Generation!
Step 1
Step 2
Step 3
Step 4
Step 5
Name
of Label1
to lblNumber
Step 6
lblNumber
and Copy it
lblNumbers
"s)
Step 7
Step 8
Name
Property of Command1
to cmdChoose
,
the Caption
Property to Choose
and the Name
of Command2
to cmdQuit
and the Caption
to Quit
Step 9
Click on the Form
and change its Name
Property to frmMain
then Double Click on the Form of frmMain
and type in the Form_Load
Sub:
Dim i As Integer
For i = 0 To 5
lblNumber(i).Caption = ""
Next i
Step 10
Double Click on the Command Button of Choose or cmdChoose
and type in the cmdChoose_Click()
Sub:
Dim arrNumber(1 To 6) As Integer ' Array
Dim intLucky As Integer ' Random Number
Dim intCount As Integer ' Selection Counter
Dim intCheck As Integer ' Previous Selection Counter
For intCount = 1 To 6 ' Select Six Numbers
Start: ' Start Point
Randomize (Timer) ' Seed Randomiser
intLucky = Int((49 * Rnd) + 1) ' Random number 1 to 49
For intCheck = 1 To 6
If intLucky = arrNumber(intCheck) Then
GoTo Start 'If selected number already present, select again
End If
Next intCheck
arrNumber(intCount) = intLucky ' Store selected number in array
lblNumber(intCount - 1) = arrNumber(intCount) ' apply selection to labels
Next intCount
Step 11
Double Click on the Command Button of Quit or cmdQuit
and type in the cmdQuit_Click()
Sub:
Unload Me
Step 12
Step 13
Step 14
You have just created a simple Random Number generator! Try adding extras such as colours and other effects and see what else you can achieve with this program.