Wednesday, October 21, 2009

Bingo

The following code is generated by me of course.
This code will make a bingo cards in a single parent form...


Public Class FrmBingoCard
Private Sub FrmBingoCard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim row, col As Integer
Dim bingo = "BINGO"
For row = 1 To 6
For col = 1 To 5
Dim lbl As New Label
With lbl
.Name = "Label" & row & col
.TextAlign = ContentAlignment.MiddleCenter
.Visible = True
.Font = New Font("Verdana", 10, FontStyle.Bold)
.BorderStyle = BorderStyle.Fixed3D
.Width = 50
.Height = 50
.Top = (row - 1) * .Width
.Left = (col - 1) * .Height
.Tag = row
.ForeColor = Color.DarkBlue
.Text = .Tag
Me.Controls.Add(lbl)
End With
Next
Next
Me.Width = 258
Me.Height = 330
Dim ctl As Control
Dim i, span As Integer
Dim subs As Char
For Each ctl In Me.Controls
If TypeOf ctl Is Label And ctl.Tag = "1" Then
ctl.Text = bingo.Substring(i, 1)
ctl.BackColor = Color.RoyalBlue
ctl.ForeColor = Color.AntiqueWhite
ctl.Font = New Font("tahoma", 15, FontStyle.Bold)
i += 1
ElseIf ctl.Name.Substring(6, 1) Then
subs = ctl.Name.Substring(6, 1)
Select Case subs
Case "1"
span = 1
Case "2"
span = 16
Case "3"
span = 31
Case "4"
span = 46
Case "5"
span = 61
End Select
Do
GenerateNumbers(span, ctl)
Loop While NumberRepetition(ctl) 'And ctl.Tag <> 1 And ctl.Tag <> 100
If ctl.Name.Substring(5, 2) = "43" Then
ctl.Text = "FREE"
'ctl.Tag = 100
ctl.BackColor = Color.DodgerBlue
ctl.ForeColor = Color.AntiqueWhite
End If
End If
Next
End Sub
Private Sub GenerateNumbers(ByVal range As Integer, ByVal lbl As Control)
Randomize()
lbl.Text = Int(Rnd() * 15) + range
End Sub
Public Sub FindNumber()
Dim lbl As Control
For Each lbl In Me.Controls
If TypeOf lbl Is Label Then
If lbl.Tag <> 1 And lbl.Tag <> 100 Then
If lbl.Text <> "FREE" Then
If lbl.Text = draw Then
lbl.BackColor = Color.DodgerBlue
lbl.ForeColor = Color.AntiqueWhite
End If
End If
End If
End If
Next
End Sub
Private Function NumberRepetition(ByVal lbl As Control) As Boolean
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is Label Then
If ctl.Tag <> 1 And ctl.Tag <> 100 And ctl.Name <> lbl.Name Then
If ctl.Text = lbl.Text Then
Return True
End If
End If
End If
Next
Return False
End Function
Public Sub HorizontalBingo()
Dim row As Integer
Dim count As Integer
Dim aligned(5) As Integer
Dim lbl As Control
For row = 2 To 6
For Each lbl In Me.Controls
If lbl.Tag = row And lbl.BackColor = Color.DodgerBlue Then
count += 1
If count = 5 Then
Form2.Timer1.Enabled = False
MessageBox.Show(Me.Text & " Won!!!" & vbCrLf & "Horizontal Bingo" & vbCrLf & "Congratulations!!!", "Won")
OtherControls()
End If
End If
Next
count = 0
Next
End Sub
Public Sub VerticalBingo()
Dim col, count, subs As Integer
Dim lbl As Control
For col = 1 To 5
For Each lbl In Me.Controls
subs = CInt(lbl.Name.Substring(6, 1))
If subs = col And lbl.Tag <> 1 And lbl.BackColor = Color.DodgerBlue Then
count += 1
If count = 5 Then
Form2.Timer1.Enabled = False
MessageBox.Show(Me.Text & " Won!!!" & vbCrLf & "Vertical Bingo" & vbCrLf & "Congratulations!!!", "Won")
OtherControls()
End If 'end count=5 if
End If ' end name.substring if
Next ' end for each
count = 0
Next 'end for
End Sub
Public Sub DiagonalBingo()
Dim array(24) As Control
Dim lbl As Control
Dim indexOfLoop As Integer
For Each lbl In Me.Controls
If TypeOf lbl Is Label And lbl.Tag <> 1 And lbl.Tag <> 100 Then
array(indexOfLoop) = lbl
indexOfLoop += 1
End If
Next
LeftToRight(array)
RightToLeftbingo(array)
End Sub
Private Sub LeftToRight(ByVal lblNames() As Control)
Dim indexOfLoop, count As Integer
For indexOfLoop = 4 To 20 Step 4
If lblNames(indexOfLoop).BackColor = Color.DodgerBlue Then
count += 1
If count > 5 Then
MessageBox.Show(Me.Text & " Won!!!" & vbCrLf & "Diagonal Bingo" & vbCrLf & "Left to Right" & vbCrLf & "Congratulations!!!", "Won")
OtherControls()
End If
End If
Next
End Sub
Private Sub RightToLeftbingo(ByVal lblNames() As Control)
Dim read As Boolean
Dim indexOfLoop, count As Integer
For indexOfLoop = 0 To 24 Step 6
If lblNames(indexOfLoop).BackColor = Color.DodgerBlue And lblNames(indexOfLoop).Tag <> 1 Then
count += 1
If count = 5 And read = False Then
MessageBox.Show(Me.Text & " Won!!!" & vbCrLf & "Diagonal Bingo" & vbCrLf & "Right to Left" & vbCrLf & "Congratulations!!!", "Won")
OtherControls()
End If
End If
Next
End Sub
Private Sub OtherControls()
Form2.btnDraw.Enabled = False
Form2.MenuToolStripMenuItem.Enabled = True
Form2.ToolStripButton1.Enabled = True
Form2.NewCardToolStripMenuItem.Enabled = False
Form2.NewGameToolStripMenuItem.Enabled = True
Form2.ToolStripButton1.Enabled = False
Form2.Timer1.Enabled = False
End Sub
End Class


While this could should be on the Parent form...

Public Class Form2
Dim gotCard As Boolean
Dim numOfCards As Integer
Dim cardNo As Integer = 1
Dim timerCount As Integer
Private Sub NewCardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewCardToolStripMenuItem.Click
Dim frmCard As New FrmBingoCard

gotCard = True
With frmCard
.MdiParent = Me
.Name = "FrmCard" & cardNo
.Text = "Card " & cardNo
.Show()
cardNo += 1
End With
numOfCards = cardNo
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
gotCard = True
NewCardToolStripMenuItem_Click(sender, e)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

lblDigit1.Text = Int(Rnd() * 9)
lblDigit2.Text = Int(Rnd() * 9)
If timerCount = 20 Then
Timer1.Enabled = False
Do
draw = Int(Rnd() * 75) + 1
Loop While IsNumberDrew(draw)
lblDigit1.Text = draw \ 10
lblDigit2.Text = draw Mod 10
FindNumberPerCard()
LetterOfDigit(draw)
timerCount = 0
End If
timerCount += 1
End Sub
Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click
If gotCard = False Then
MessageBox.Show("You need to have a Bingo Card", "Bingo Card needed")
Else
Timer1.Enabled = True
MenuToolStripMenuItem.Enabled = False
ToolStripButton1.Enabled = False
End If
End Sub
Private Sub LetterOfDigit(ByVal number)
If number > 0 And number < 16 Then
txtB.Text &= vbCrLf & number
ElseIf number > 15 And number < 31 Then
txtI.Text &= vbCrLf & number
ElseIf number > 29 And number < 46 Then
txtN.Text &= vbCrLf & number
ElseIf number > 44 And number < 61 Then
txtG.Text &= vbCrLf & number
Else
txtO.Text &= vbCrLf & number
End If
End Sub
Private Sub FindNumberPerCard()
Dim frm As FrmBingoCard
For Each frm In Me.MdiChildren
frm.FindNumber()
frm.HorizontalBingo()
frm.VerticalBingo()
frm.DiagonalBingo()
Next
End Sub
Private Function IsNumberDrew(ByVal number As Integer) As Boolean
If number > 0 And number And txtB.Text.Contains(number) Then
Return True
ElseIf number > 15 And number < 31 And txtI.Text.Contains(number) Then
Return True
ElseIf number > 29 And number < 46 And txtN.Text.Contains(number) Then
Return True
ElseIf number > 44 And number < 61 And txtG.Text.Contains(number) Then
Return True
ElseIf number > 59 And number < 76 And txtO.Text.Contains(number) Then
Return True
End If
Return False
End Function
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ToolStrip1.Items(0).Image = ImageList1.Images(0)
NewGameToolStripMenuItem.Enabled = False
End Sub
Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
Dim frm As FrmBingoCard
txtB.Text = ""
txtI.Text = ""
txtN.Text = ""
txtG.Text = ""
txtO.Text = ""
cardNo = 1
btnDraw.Enabled = True
gotCard = False
lblDigit1.Text = ""
lblDigit2.Text = ""
NewCardToolStripMenuItem.Enabled = True
NewGameToolStripMenuItem.Enabled = False
ToolStripButton1.Enabled = True
For Each frm In Me.MdiChildren
frm.Close()
Next
End Sub
End Class


every form has a mdiParent/mdiParent properties that would specify which form is its parent and child...
To Test my created program, follow this link...
http://www.missupload.com/ccw7hchfvgj6/ProExer6.exe.html

Catch Game Program

This program would allow you to make a simple game, in this manner this is a catch game.
This game will drop a smiley gif that will catched by a car. The only object that you can control is the car by pressing left and right keys...
The Code for controlling the car is as follows...


Select Case e.KeyCode
Case Keys.Right
If car.Left <= (Me.Width - car.Width) - 12 Then
car.Left += 10
End If
Case Keys.Left
If car.Left >= 0 Then
car.Left -= 10
End If
End Select


This code will be paste on the keydown event of the form.
to show the sample follow this link...
http://www.missupload.com/u9hnq05g8j77/ProExer4.exe.html

KeyPress Event

Controlling the keypress event by letting the user input only numbers.
This code let the user input only the numbers and backspace not the characters / letters.

Select Case e.KeyChar
Case CChar("0") To CChar("9"), CChar("."), ControlChars.Back
e.Handled = False
Case ControlChars.Cr
txtPrice.Focus()
e.Handled = False
Case Else
e.Handled = True


End Select

follow this link for the sample program...
http://www.missupload.com/t1cuuf31d9ue/ProExer3.exe.html