This is another simple game allowing you to choose your Lotto Numbers, features Drawing, Coloured Balls, Random Number generation and more!
Printer Friendly Download Tutorial (149KB) Download Source Code (15KB)
Step 1
Start Microsoft Visual Basic 2005 Express Edition, then select File then New Project... Choose Windows Application from the New Project Window, enter a name for the Project and then click OK, see below:
![New Project](tutorials/vbexnewproject.gif)
Step 2
A Blank Form named Form1 should then appear, see below:
![Form1](tutorials/vbexform1.gif)
Step 3
Then from the Windows Forms components tab select the PictureBox control:
![PictureBox Component](tutorials/vbexpicturebox.gif)
Step 4
Draw a PictureBox on the Form as indicated below:
![PictureBox on Form1](tutorials/vbexlottopic.gif)
Step 5
Then from the Windows Forms components tab select the Button control:
![Button Component](tutorials/vbexbutton.gif)
Step 6
Draw a Button on the Form, see below:
![Form1 with PictureBox and Button](tutorials/vbexlottobtn.gif)
Step 7
Then goto the Properties box and change the Text property to Choose, see below:
![Button Properties](tutorials/vbexlottobtnprop.gif)
Step 8
Right click on the Form and select the View Code option then in the code view type in below Public Class Form1 the following:
Dim DrawBrush As Brush Dim DrawBitmap As Bitmap Dim DrawGraphics As Graphics Dim MyFont As New System.Drawing.Font("Arial", 12, _ FontStyle.Regular, GraphicsUnit.Point) Dim arrNumber(0 To 5) As Integer Dim intNumber As Integer
See Below:
![Lotto Declarations](tutorials/vbexlottodecs.gif)
Step 9
While still in the Code view type below the Declarations entered in the previous step (anything starting with Dim) the following:
' Selects Numbers Private Sub ChooseNumbers() Dim x, y As Integer For x = 0 To 5 Start: Randomize() intNumber = Int((49 * Rnd()) + 1) ' Random number 1 to 49 For y = 0 To 5 ' If number already been selected select another one If intNumber = arrNumber(y) Then GoTo Start End If Next y arrNumber(x) = intNumber Next x End Sub
See Below:
![Lotto Choose Method](tutorials/vbexlottochoose.gif)
Step 10
After the ChooseNumbers() Sub has been typed in, type below it the following:
' Determines what colour the ball should be based on the Value Private Function BallColour(ByVal Value As Integer) As Brush If Value > 0 And Value < 10 Then DrawBrush = Brushes.White ElseIf Value > 10 And Value < 20 Then DrawBrush = Brushes.SkyBlue ElseIf Value > 20 And Value < 30 Then DrawBrush = Brushes.Magenta ElseIf Value > 30 And Value < 40 Then DrawBrush = Brushes.LawnGreen ElseIf Value > 40 And Value < 50 Then DrawBrush = Brushes.Yellow End If Return DrawBrush End Function
See Below:
![Lotto Colour Method](tutorials/vbexlottocolour.gif)
Step 11
After the BallColour() Function type in the following ball rendering routines:
' Draws a Ball with a colour and value Private Sub DrawBall(ByVal Number As String, ByVal Left As Integer) Dim pos As Integer If Number.Length = 2 Then pos = 9 Else pos = 13 ' Postion for Single or Double Digit DrawGraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality ' Smooth Drawing DrawGraphics.FillEllipse(BallColour(CInt(Number)), Left, 1, 40, 40) ' Draw Ball DrawGraphics.DrawString(Number, MyFont, Brushes.Black, Left + pos, 13) ' Draw Number End Sub ' Outputs all Balls to the PictureBox Private Sub RenderBalls() Dim i As Integer Dim intLeft As Integer intLeft = 1 DrawBitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height) DrawGraphics = Graphics.FromImage(DrawBitmap) For i = 0 To 5 DrawBall(arrNumber(i), intLeft) intLeft = intLeft + 45 Next PictureBox1.Image = DrawBitmap End Sub
See Below:
![Lotto Ball Routines](tutorials/vbexlottoballsubs.gif)
Step 12
Right click on the Code View and select the View Designer option then Double Click on the Button and type the following in Button1_Click method:
ChooseNumbers() RenderBalls()
See Below:
![Button1 Click Routine](tutorials/vbexlottobtnclick.gif)
Step 13
Save the Project as you have now finished the application, then click on Start:
![Start](tutorials/vbexstart.gif)
When you do the following will appear:
![Application Running](tutorials/vbexlottorun.gif)
Step 14
Click the Choose button to select your Lotto Numbers, see below:
![Choose Lotto Numbers](tutorials/vbexlotto.gif)
Step 15
Click on the Close button on the top right of Form1 to end the application.
You have just created a simple Lotto Number selector featuring a random number generator and graphics! You could customise the ball colours the ones selected are for the UK Lottery if your local Lottery has different ball colours, try customising this program to match those. You can make many changes such as showing the balls in ascending order and more!