Lucky Lotto

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

Step 2

A Blank Form named Form1 should then appear, see below:

Form 1

Step 3

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

Label Control

Step 4

Draw a Label on the Form as shown below:

Form with Label

Step 5

Change the name of Label1 to lblNumber see below:

Label Properties

Step 6

Click on the Label (lblNumber) and Copy Copy it, then click on the Form, then click Paste 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

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, as shown below:

Form with Labels and Command Buttons

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 Start / Run or Press F5 and the below will appear:

Lucky Lotto Running

Step 13

Click on Choose to select a set six of numbers, displayed in the labels, see example below:

Lucky Lotto Selected Numbers

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.

Copyright Comentsys © 1997 - 2009, All rights reserved. About | Contact | Link to Page
Valid XHTML 1.1! Valid CSS Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0 This website is ICRA rated