Deal or no Deal VB Tutorial
In order to make this game from scrath you will need to add the following and alter the settings as follows:
Add 23 buttons to your form (These make your boxes). To make this very easy just drag your mouse to select all 23 boxes at once.
Now go back onto your forms design screen and create a picturebox control somewhere on the form:
You can copy and paste this code directly into your form if you wish and it should function ok.
Public Class Form1
'This section of code handles the main game functions
Public start = 0 'Set to "0" because the game hasnt started yet
Public bchosen = -1 'Counts how many boxes have been opened until we reach the last 2
Public bxclick = 0 'Counts how many boxes have been opened before the banker makes an offer.....it goes to "3"
Public haltgame = 0 'Pauses the game and locks all boxes while you decide to deal or no deal
Public Number1 As Integer
Public Number2 As Integer
Public Number3 As Integer
Public Number4 As Integer 'All of these store each boxes value and are used by the banker to generate an offer
Public Number5 As Integer
Public Number6 As Integer
Public Number7 As Integer
Public Number8 As Integer
Public Number9 As Integer
Public Number10 As Integer
Public Number11 As Integer
Public Number12 As Integer
Public Number13 As Integer
Public Number14 As Integer
Public Number15 As Integer
Public Number16 As Integer
Public Number17 As Integer
Public Number18 As Integer
Public Number19 As Integer
Public Number20 As Integer
Public Number21 As Integer
Public Number22 As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'This runs when the game loads and activates the random box generator
bvalues()
End Sub
Public Sub bvalues()
Dim myBoxv As New List(Of String)
Dim myNewBoxv As New List(Of String)
'Randomises 22 values as shown in the string below
myBoxv.AddRange(New String() {"0.1", "0.10", "0.50", "1", "5", "10", "50", "100", "250", "500", "750", "1000", "3000", "5000", "10000", "15000", "20000", "35000", "50000", "75000", "100000", "250000"})
Dim Rnd As New Random
Dim Temp As Integer
For count As Integer = 1 To 22
Temp = Rnd.Next(0, myBoxv.Count)
myNewBoxv.Add(myBoxv(Temp))
myBoxv.RemoveAt(Temp)
Next
'When the values have been generated they are placed randomly into all 22 boxes
'values are stored in each boxes TAG propertie
Button1.Tag = myNewBoxv(0)
Button2.Tag = myNewBoxv(1)
Button3.Tag = myNewBoxv(2)
Button4.Tag = myNewBoxv(3)
Button5.Tag = myNewBoxv(4)
Button6.Tag = myNewBoxv(5)
Button7.Tag = myNewBoxv(6)
Button8.Tag = myNewBoxv(7)
Button9.Tag = myNewBoxv(8)
Button10.Tag = myNewBoxv(9)
Button11.Tag = myNewBoxv(10)
Button12.Tag = myNewBoxv(11)
Button13.Tag = myNewBoxv(12)
Button14.Tag = myNewBoxv(13)
Button15.Tag = myNewBoxv(14)
Button16.Tag = myNewBoxv(15)
Button17.Tag = myNewBoxv(16)
Button18.Tag = myNewBoxv(17)
Button19.Tag = myNewBoxv(18)
Button20.Tag = myNewBoxv(19)
Button21.Tag = myNewBoxv(20)
Button22.Tag = myNewBoxv(21)
End Sub
Private Sub Button7_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button7.MouseClick, Button15.MouseClick, Button13.MouseClick, Button9.MouseClick, Button8.MouseClick, Button6.MouseClick, Button5.MouseClick, Button4.MouseClick, Button3.MouseClick, Button22.MouseClick, Button21.MouseClick, Button20.MouseClick, Button2.MouseClick, Button19.MouseClick, Button18.MouseClick, Button17.MouseClick, Button16.MouseClick, Button14.MouseClick, Button12.MouseClick, Button11.MouseClick, Button10.MouseClick, Button1.MouseClick
'This control the mouse click event for all 22 buttons
'its quicker than codeing each button individually
'you can use this method for other controls aswell
'We dont want the first box we chose to show its value...we are simply selecting it as our keep box
If start = 0 Then 'If the game hasnt started yet we chose the box and store its value in the keeper box
Button23.Tag = CType(sender, Button).Tag
Button23.Text = " " + CType(sender, Button).Text
CType(sender, Button).Visible = False 'We make our selected box invisible
bchosen += 1
End If
'The game has now started and we can open boxes ..start is set to "1"
If start = 1 And bchosen < 20 And haltgame = 0 Then
'We open whichever boxed we clicked and show its value
CType(sender, Button).BackColor = Color.White
CType(sender, Button).FlatAppearance.BorderColor = Color.Red
CType(sender, Button).BackgroundImage = Nothing 'Removes the box picture
CType(sender, Button).FlatAppearance.BorderSize = 2
CType(sender, Button).Text = "£" + CType(sender, Button).Tag
CType(sender, Button).Enabled = False
'We set the boxes value to "0" so the banker knows its no longer there and adjusts his next offer
CType(sender, Button).Tag = 0
bchosen += 1 'We have clicked a box so we add it to the total - goes to 20
bxclick += 1 'We have opened a box so add it to the total - goes to 3
End If
'Because we needed to calculate 1p - 10p - 50p we dont want it to appear on a box like £0.10p
'so if a box contains this amount we change it to 10p ..it looks better!
If CType(sender, Button).Text = "£0.1" And haltgame = 0 Then
CType(sender, Button).Text = "1p"
CType(sender, Button).FlatAppearance.BorderColor = Color.Blue
ElseIf CType(sender, Button).Text = "£0.10" And haltgame = 0 Then
CType(sender, Button).Text = "10p"
CType(sender, Button).FlatAppearance.BorderColor = Color.Blue
ElseIf CType(sender, Button).Text = "£0.50" And haltgame = 0 Then
CType(sender, Button).Text = "50p"
CType(sender, Button).FlatAppearance.BorderColor = Color.Blue
'If the box has a value less than 1000 then its a blue box and we change the border to blue color
'otherwise it stays red because its a high value
ElseIf CType(sender, Button).Text < 1000 And haltgame = 0 Then
CType(sender, Button).FlatAppearance.BorderColor = Color.Blue
Else
End If
'If we have opened 20 boxes then we activate our keeper box
If bchosen = 20 Then
Button23.Enabled = True
bxclick = 3 'We set this immediately to "3" because we only have 2 boxes left and we want the banker to make an offer
End If
If bxclick = 3 Then
haltgame = 1 'Game is paused
bankeroffer() 'Banker makes an offer
End If
start = 1 'Game has started
End Sub
Public Sub bankeroffer()
'These retrieve each remaining boxes value so the banker can calculate an offer
Number1 = Button1.Tag
Number2 = Button2.Tag
Number3 = Button3.Tag
Number4 = Button4.Tag
Number5 = Button5.Tag
Number6 = Button6.Tag
Number7 = Button7.Tag
Number8 = Button8.Tag
Number9 = Button9.Tag
Number10 = Button10.Tag
Number11 = Button11.Tag
Number12 = Button12.Tag
Number13 = Button13.Tag
Number14 = Button14.Tag
Number15 = Button15.Tag
Number16 = Button16.Tag
Number17 = Button17.Tag
Number18 = Button18.Tag
Number19 = Button19.Tag
Number20 = Button20.Tag
Number21 = Button21.Tag
Number22 = Button22.Tag
'We add all of the remaining values together
Dim TotalNumber As Integer = Number1 + Number2 + Number3 + Number4 + Number5 + Number6 + Number7 + Number8 + Number8 + Number10 + Number11 + Number12 + Number13 + Number14 + Number15 + Number16 + Number17 + Number18 + Number19 + Number20 + Number21 + Number22
Dim Average As Integer
Average = TotalNumber / 22 'We now calculate the average value of all those values
PictureBox1.Image = My.Resources.utilphone1 'We animate the telephone
TextBox1.Visible = True
TextBox1.Text = "£" + Average.ToString() 'Place the offer in a string
bxclick = 0
My.Computer.Audio.Play(My.Resources.phone06, AudioPlayMode.BackgroundLoop) 'Banker makes a phone call
End Sub
Private Sub Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button24.Click
'We accept the bankers offer and make the "play again" button visible
Label1.Text = " Congratulations!!"
Button26.Visible = True
End Sub
Private Sub Button25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button25.Click
'We reject the bankers off and make it go away
Panel1.Visible = False
'Are we on the last 2 boxes?..Pause the game and activate the keeper box to see what you won
If bchosen = 21 Then
Button23.Enabled = True
bchosen = 25
haltgame = 1
Else
haltgame = 0 'If we have more than 2 boxes left then keep playing
End If
End Sub
Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click
'We click our keeper box and show its value
CType(sender, Button).BackColor = Color.White
CType(sender, Button).ForeColor = Color.Red
CType(sender, Button).BackgroundImage = Nothing
CType(sender, Button).FlatAppearance.BorderSize = 2
CType(sender, Button).Text = "£" + CType(sender, Button).Tag
CType(sender, Button).Enabled = False
Panel1.Visible = True
Label1.Text = " Congratulations!!"
Button24.Visible = False
Button25.Visible = False
Button26.Visible = True
TextBox1.Text = "£" + CType(sender, Button).Tag
If CType(sender, Button).Tag = "£0.1" Then
CType(sender, Button).Text = "1p"
ElseIf CType(sender, Button).Tag = "£0.10" Then
CType(sender, Button).Text = "10p"
ElseIf CType(sender, Button).Tag = "£0.50" Then
CType(sender, Button).Text = "50p"
ElseIf CType(sender, Button).Tag < 1000 Then
CType(sender, Button).ForeColor = Color.Blue
Else
bchosen = 20
haltgame = 1
End If
End Sub
Private Sub Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button26.Click
'If we want to play again we shutdown the program and immediately restart it
Process.Start("Vdealornodeal")
Me.Close()
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
'We have answered the phone and stopped it ringing
My.Computer.Audio.Stop()
If haltgame = 1 Then 'Stop the phone moving and show the bankers offer
PictureBox1.Image = My.Resources.utilphone2_05
Panel1.Visible = True
End If
End Sub
End Class
And thats all you need. Please dont forget to rate the video and hope to see you again soon :). Contact me if you need any help.
Add 23 buttons to your form (These make your boxes). To make this very easy just drag your mouse to select all 23 boxes at once.
- Change FlatStyle to 'Flat'
- Change backgroundimage to the box picture
- Change font size to around 12-14
- Change TextAlign to MiddleCenter
- Change the Text for each of the boxes to "1".."2"..."3"..and so on to 22 (leaving 1 box blank)
- Change Enabled to 'False'
- Change the picturebox image to 'stationary image file'
- Label - Change the text to 'I will buy your box for:"
- A large Textbox with multiline ticked
- 2 buttons - 1 text "deal"..the other "no deal"
- Change the panel visible propertie to 'False'
- Change button text to 'Play again?'
- Change the visible propertie to 'False'
Now go back onto your forms design screen and create a picturebox control somewhere on the form:
- Change image to the background image (brickwall)
- Right click inside the picturebox and click on 'Send to back'
- Then click the little arrow on the top right corner and click "Dock in parent container"
You can copy and paste this code directly into your form if you wish and it should function ok.
Public Class Form1
'This section of code handles the main game functions
Public start = 0 'Set to "0" because the game hasnt started yet
Public bchosen = -1 'Counts how many boxes have been opened until we reach the last 2
Public bxclick = 0 'Counts how many boxes have been opened before the banker makes an offer.....it goes to "3"
Public haltgame = 0 'Pauses the game and locks all boxes while you decide to deal or no deal
Public Number1 As Integer
Public Number2 As Integer
Public Number3 As Integer
Public Number4 As Integer 'All of these store each boxes value and are used by the banker to generate an offer
Public Number5 As Integer
Public Number6 As Integer
Public Number7 As Integer
Public Number8 As Integer
Public Number9 As Integer
Public Number10 As Integer
Public Number11 As Integer
Public Number12 As Integer
Public Number13 As Integer
Public Number14 As Integer
Public Number15 As Integer
Public Number16 As Integer
Public Number17 As Integer
Public Number18 As Integer
Public Number19 As Integer
Public Number20 As Integer
Public Number21 As Integer
Public Number22 As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'This runs when the game loads and activates the random box generator
bvalues()
End Sub
Public Sub bvalues()
Dim myBoxv As New List(Of String)
Dim myNewBoxv As New List(Of String)
'Randomises 22 values as shown in the string below
myBoxv.AddRange(New String() {"0.1", "0.10", "0.50", "1", "5", "10", "50", "100", "250", "500", "750", "1000", "3000", "5000", "10000", "15000", "20000", "35000", "50000", "75000", "100000", "250000"})
Dim Rnd As New Random
Dim Temp As Integer
For count As Integer = 1 To 22
Temp = Rnd.Next(0, myBoxv.Count)
myNewBoxv.Add(myBoxv(Temp))
myBoxv.RemoveAt(Temp)
Next
'When the values have been generated they are placed randomly into all 22 boxes
'values are stored in each boxes TAG propertie
Button1.Tag = myNewBoxv(0)
Button2.Tag = myNewBoxv(1)
Button3.Tag = myNewBoxv(2)
Button4.Tag = myNewBoxv(3)
Button5.Tag = myNewBoxv(4)
Button6.Tag = myNewBoxv(5)
Button7.Tag = myNewBoxv(6)
Button8.Tag = myNewBoxv(7)
Button9.Tag = myNewBoxv(8)
Button10.Tag = myNewBoxv(9)
Button11.Tag = myNewBoxv(10)
Button12.Tag = myNewBoxv(11)
Button13.Tag = myNewBoxv(12)
Button14.Tag = myNewBoxv(13)
Button15.Tag = myNewBoxv(14)
Button16.Tag = myNewBoxv(15)
Button17.Tag = myNewBoxv(16)
Button18.Tag = myNewBoxv(17)
Button19.Tag = myNewBoxv(18)
Button20.Tag = myNewBoxv(19)
Button21.Tag = myNewBoxv(20)
Button22.Tag = myNewBoxv(21)
End Sub
Private Sub Button7_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button7.MouseClick, Button15.MouseClick, Button13.MouseClick, Button9.MouseClick, Button8.MouseClick, Button6.MouseClick, Button5.MouseClick, Button4.MouseClick, Button3.MouseClick, Button22.MouseClick, Button21.MouseClick, Button20.MouseClick, Button2.MouseClick, Button19.MouseClick, Button18.MouseClick, Button17.MouseClick, Button16.MouseClick, Button14.MouseClick, Button12.MouseClick, Button11.MouseClick, Button10.MouseClick, Button1.MouseClick
'This control the mouse click event for all 22 buttons
'its quicker than codeing each button individually
'you can use this method for other controls aswell
'We dont want the first box we chose to show its value...we are simply selecting it as our keep box
If start = 0 Then 'If the game hasnt started yet we chose the box and store its value in the keeper box
Button23.Tag = CType(sender, Button).Tag
Button23.Text = " " + CType(sender, Button).Text
CType(sender, Button).Visible = False 'We make our selected box invisible
bchosen += 1
End If
'The game has now started and we can open boxes ..start is set to "1"
If start = 1 And bchosen < 20 And haltgame = 0 Then
'We open whichever boxed we clicked and show its value
CType(sender, Button).BackColor = Color.White
CType(sender, Button).FlatAppearance.BorderColor = Color.Red
CType(sender, Button).BackgroundImage = Nothing 'Removes the box picture
CType(sender, Button).FlatAppearance.BorderSize = 2
CType(sender, Button).Text = "£" + CType(sender, Button).Tag
CType(sender, Button).Enabled = False
'We set the boxes value to "0" so the banker knows its no longer there and adjusts his next offer
CType(sender, Button).Tag = 0
bchosen += 1 'We have clicked a box so we add it to the total - goes to 20
bxclick += 1 'We have opened a box so add it to the total - goes to 3
End If
'Because we needed to calculate 1p - 10p - 50p we dont want it to appear on a box like £0.10p
'so if a box contains this amount we change it to 10p ..it looks better!
If CType(sender, Button).Text = "£0.1" And haltgame = 0 Then
CType(sender, Button).Text = "1p"
CType(sender, Button).FlatAppearance.BorderColor = Color.Blue
ElseIf CType(sender, Button).Text = "£0.10" And haltgame = 0 Then
CType(sender, Button).Text = "10p"
CType(sender, Button).FlatAppearance.BorderColor = Color.Blue
ElseIf CType(sender, Button).Text = "£0.50" And haltgame = 0 Then
CType(sender, Button).Text = "50p"
CType(sender, Button).FlatAppearance.BorderColor = Color.Blue
'If the box has a value less than 1000 then its a blue box and we change the border to blue color
'otherwise it stays red because its a high value
ElseIf CType(sender, Button).Text < 1000 And haltgame = 0 Then
CType(sender, Button).FlatAppearance.BorderColor = Color.Blue
Else
End If
'If we have opened 20 boxes then we activate our keeper box
If bchosen = 20 Then
Button23.Enabled = True
bxclick = 3 'We set this immediately to "3" because we only have 2 boxes left and we want the banker to make an offer
End If
If bxclick = 3 Then
haltgame = 1 'Game is paused
bankeroffer() 'Banker makes an offer
End If
start = 1 'Game has started
End Sub
Public Sub bankeroffer()
'These retrieve each remaining boxes value so the banker can calculate an offer
Number1 = Button1.Tag
Number2 = Button2.Tag
Number3 = Button3.Tag
Number4 = Button4.Tag
Number5 = Button5.Tag
Number6 = Button6.Tag
Number7 = Button7.Tag
Number8 = Button8.Tag
Number9 = Button9.Tag
Number10 = Button10.Tag
Number11 = Button11.Tag
Number12 = Button12.Tag
Number13 = Button13.Tag
Number14 = Button14.Tag
Number15 = Button15.Tag
Number16 = Button16.Tag
Number17 = Button17.Tag
Number18 = Button18.Tag
Number19 = Button19.Tag
Number20 = Button20.Tag
Number21 = Button21.Tag
Number22 = Button22.Tag
'We add all of the remaining values together
Dim TotalNumber As Integer = Number1 + Number2 + Number3 + Number4 + Number5 + Number6 + Number7 + Number8 + Number8 + Number10 + Number11 + Number12 + Number13 + Number14 + Number15 + Number16 + Number17 + Number18 + Number19 + Number20 + Number21 + Number22
Dim Average As Integer
Average = TotalNumber / 22 'We now calculate the average value of all those values
PictureBox1.Image = My.Resources.utilphone1 'We animate the telephone
TextBox1.Visible = True
TextBox1.Text = "£" + Average.ToString() 'Place the offer in a string
bxclick = 0
My.Computer.Audio.Play(My.Resources.phone06, AudioPlayMode.BackgroundLoop) 'Banker makes a phone call
End Sub
Private Sub Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button24.Click
'We accept the bankers offer and make the "play again" button visible
Label1.Text = " Congratulations!!"
Button26.Visible = True
End Sub
Private Sub Button25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button25.Click
'We reject the bankers off and make it go away
Panel1.Visible = False
'Are we on the last 2 boxes?..Pause the game and activate the keeper box to see what you won
If bchosen = 21 Then
Button23.Enabled = True
bchosen = 25
haltgame = 1
Else
haltgame = 0 'If we have more than 2 boxes left then keep playing
End If
End Sub
Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click
'We click our keeper box and show its value
CType(sender, Button).BackColor = Color.White
CType(sender, Button).ForeColor = Color.Red
CType(sender, Button).BackgroundImage = Nothing
CType(sender, Button).FlatAppearance.BorderSize = 2
CType(sender, Button).Text = "£" + CType(sender, Button).Tag
CType(sender, Button).Enabled = False
Panel1.Visible = True
Label1.Text = " Congratulations!!"
Button24.Visible = False
Button25.Visible = False
Button26.Visible = True
TextBox1.Text = "£" + CType(sender, Button).Tag
If CType(sender, Button).Tag = "£0.1" Then
CType(sender, Button).Text = "1p"
ElseIf CType(sender, Button).Tag = "£0.10" Then
CType(sender, Button).Text = "10p"
ElseIf CType(sender, Button).Tag = "£0.50" Then
CType(sender, Button).Text = "50p"
ElseIf CType(sender, Button).Tag < 1000 Then
CType(sender, Button).ForeColor = Color.Blue
Else
bchosen = 20
haltgame = 1
End If
End Sub
Private Sub Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button26.Click
'If we want to play again we shutdown the program and immediately restart it
Process.Start("Vdealornodeal")
Me.Close()
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
'We have answered the phone and stopped it ringing
My.Computer.Audio.Stop()
If haltgame = 1 Then 'Stop the phone moving and show the bankers offer
PictureBox1.Image = My.Resources.utilphone2_05
Panel1.Visible = True
End If
End Sub
End Class
And thats all you need. Please dont forget to rate the video and hope to see you again soon :). Contact me if you need any help.