VB Lucky Draw Application
A fun game created using Visual Basic 6 and VB.NET with random prize selection
What is Lucky Draw Application?
This is an application that simulates a lucky draw game. This program consists of a 3x3 matrix, which means there are 9 command buttons in a grid. The command buttons are created as controls in an array, and they are differentiated by their indices. One of the buttons contains a prize; when you click on it, it displays the word "prize" on the caption. If you don't strike the prize, the word "The Prize is here!" will appear on the command button that contains the prize.
We use a randomize process to create the chance events. Random integers from 1 to 9 can be created using the statement n=Int(Rnd*9)+1. Int is a function that converts numbers to integers and Rnd is a method that generates random numbers between 0 and 1. When the user clicks a command button, a random number between 0 and 9 is generated. If this number corresponds to the index of the command button, it will show the word "Prize" on the clicked command button, otherwise, it shows the words "The Prize is here!" on the command button with an index corresponding to the generated number.
Interactive Demo
Try the Lucky Draw game right here in your browser! Click on any box to see if you've won the prize.
Program Interface
The Lucky Draw program interface with a 3x3 grid of buttons
Implementation Code
Below you'll find the implementation for both Visual Basic 6 and VB.NET.
Visual Basic 6 Code
Private Sub Command1_Click(Index As Integer)
Dim n As Integer
For n = 0 To 8
Command1(n).Caption = ""
Next
n = Int(Rnd * 9)
If Index = n Then
Command1(n).Caption = "Prize"
Else
Command1(n).Caption = "The Prize is here!"
End If
End Sub
This VB6 code uses a control array of buttons. When any button is clicked:
- All button captions are cleared
- A random number between 0 and 8 is generated
- If the clicked button's index matches the random number, it shows "Prize"
- Otherwise, the button with the random index shows "The Prize is here!"
VB.NET Code
Public Class Form1
Private Sub Button_Click(sender As Object, e As EventArgs) Handles _
Button1.Click, Button2.Click, Button3.Click, _
Button4.Click, Button5.Click, Button6.Click, _
Button7.Click, Button8.Click, Button9.Click
' Clear all buttons
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
ctrl.Text = ""
End If
Next
' Generate random number
Dim rand As New Random()
Dim prizeIndex As Integer = rand.Next(0, 9)
' Get clicked button
Dim clickedButton As Button = DirectCast(sender, Button)
Dim buttonIndex As Integer = Integer.Parse(clickedButton.Name.Substring(6)) - 1
' Check if the clicked button has the prize
If buttonIndex = prizeIndex Then
clickedButton.Text = "Prize!"
Else
' Find and show the prize button
Dim prizeButton As Button = DirectCast(Me.Controls("Button" & (prizeIndex + 1).ToString()), Button)
prizeButton.Text = "The Prize is here!"
End If
End Sub
End Class
This VB.NET implementation:
- Uses a single event handler for all buttons
- Clears all button texts before each draw
- Generates a random prize location using the Random class
- Compares the clicked button with the prize location
- Shows appropriate messages based on the result
VB.NET Code
Public Class Form1
Private Sub Button_Click(sender As Object, e As EventArgs) Handles _
Button1.Click, Button2.Click, Button3.Click, _
Button4.Click, Button5.Click, Button6.Click, _
Button7.Click, Button8.Click, Button9.Click
' Clear all buttons
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
ctrl.Text = ""
End If
Next
' Generate random number
Dim rand As New Random()
Dim prizeIndex As Integer = rand.Next(0, 9)
' Get clicked button
Dim clickedButton As Button = DirectCast(sender, Button)
Dim buttonIndex As Integer = Integer.Parse(clickedButton.Name.Substring(6)) - 1
' Check if the clicked button has the prize
If buttonIndex = prizeIndex Then
clickedButton.Text = "Prize!"
Else
' Find and show the prize button
Dim prizeButton As Button = DirectCast(Me.Controls("Button" & (prizeIndex + 1).ToString()), Button)
prizeButton.Text = "The Prize is here!"
End If
End Sub
End Class
This VB.NET implementation:
- Uses a single event handler for all buttons
- Clears all button texts before each draw
- Generates a random prize location using the Random class
- Compares the clicked button with the prize location
- Shows appropriate messages based on the result
Key Programming Concepts
Random Number Generation
Using Rnd in VB6 and Random class in VB.NET to create unpredictable outcomes
Control Arrays
Managing multiple controls with the same event handler (VB6) or shared handler (VB.NET)
Event Handling
Responding to user actions (button clicks) to trigger game logic
State Management
Resetting the game state between plays for a fresh experience
How to Implement in Your Project
VB6 Implementation Steps
- Create a new Standard EXE project
- Add a 3x3 grid of CommandButtons to your form
- Set all buttons to have the same name (Command1) to create a control array
- Copy the provided VB6 code into the button click event handler
- Run the project and test the game
VB.NET Implementation Steps
- Create a new Windows Forms App (.NET Framework)
- Add 9 buttons named Button1 to Button9 in a 3x3 grid
- Set the Click event for all buttons to the same handler
- Copy the provided VB.NET code into the event handler
- Run the application and enjoy the game