This is a simple program, which will introduce you to Control Arrays, Standard Arrays, Label Control and Random Number Generation!
Printer Friendly Download Tutorial (48KB) Download Source Code (7KB)
Step 1
Load Microsoft Visual Basic, then select Standard EXE Project if VB5/6, click on the option then click Open, see below:
![New Project](tutorials/fignewproject.gif)
Step 2
A Blank Form named Form1 should then appear, see below:
![Form 1](tutorials/figform1.gif)
Step 3
Then from the Visual Basic Components Menu Select the Label Control:
![Label Control](tutorials/figlbl.gif)
Step 4
Draw a Label on the Form as shown below:
![Form with Label](tutorials/figfrmlbl.gif)
Step 5
Change the name of Label1 to lblNumber see below:
![Label Properties](tutorials/figlblprop.gif)
Step 6
Click on the Label (lblNumber) and Copy
it, then click on the Form, then click Paste
with the dialog "You already have a Conrol named lblNumber. Do you want to create a control array?" click Yes
and continue to Paste, but click on the Form between each Paste and reposition them, so that there are five
copies (Six "lblNumber"s) as shown below:
![Six lblNumbers](tutorials/figfrmlabels.gif)
Step 7
Then from the Visual Basic Components Menu Select the Command Button Control:
![Command Button Control](tutorials/figcommand.gif)
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, as shown below:
![Form with Labels and Command Buttons](tutorials/figfrmlblcmd.gif)
Step 9
Click on the Form and change it's Name Property to frmMain then Double Click on the Form (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 "Choose" (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 "Quit" (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
or Press F5 and the below will appear:
![Lucky Lotto Running](tutorials/figlottorun.gif)
Step 13
Click on Choose to select a set six of numbers, displayed in the labels, see example below:
![Lucky Lotto Selected Numbers](tutorials/figlottosel.gif)
Step 14
To Quit click on the Button Labeled Quit.
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.