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

Load Microsoft Visual Basic, then select Standard EXE Project if VB5/6, click on the option then click Open
New Project

Step 2

A Blank Form named Form1 should then appear
Blank Form

Step 3

Then from the Visual Basic Components Menu Select the Label Control:
Label Control

Step 4

Draw a Label on the Form
Form with Label Control

Step 5

Change the Name of Label1 to lblNumber
Label Name Property

Step 6

Click on the Label of lblNumber and Copy it
Copy
Then Click on the Form, then Click on Paste. with the dialog "You already have a Control named lblNumber. Do you want to create a control array?" click Yes
Paste
Continue to Paste, but Click on the Form between each Paste and reposition them so that there are five copies (Six "lblNumbers"s)
Form with Six Labels

Step 7

Then from the Visual Basic Components Menu Select the Command Button Control:
Command Button Control

Step 8

Draw Two Command Buttons on the Form change the Name Property of Command1 to cmdChoose, the Caption Property to Choose and the Name of Command2 to cmdQuit and the Caption to Quit
Form with Six Labels and Two Command Buttons

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

Save the Project, for example prjLuckyLotto, into a vacant folder as you have finished the application. Click on Start / Run
Start / Run
This should Start the application
Lucky Lotto Running

Step 13

Click on Choose to select a set six of numbers, displayed in the Labels
Lucky Lotto Chosen

Step 14

To Exit the application, Click on Quit or End / Stop
End / Stop

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.