Public Class Form1
Dim lblDice1, lblDice2, lblDice3, lblDice4, lblDice5 As New Label
Dim WithEvents butRoll As New Button
Dim nYatzee, nFourOfAKind, nThreeOfAKind As New Integer
Dim lblYatzee, lblFourOfAKind, lblThreeOfAKind As New TextBox
Dim rnd As New Random
Private Sub addDice(ByRef lbl As Label, ByVal x As Integer, ByVal y As Integer)
lbl.Text = 0
lbl.Location = New Point(x, y)
lbl.Font = New Drawing.Font("Microsoft Sans Serif", 28.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
lbl.Height = 40
lbl.Width = 40
Me.Controls.Add(lbl)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
addDice(lblDice1, 10, 20)
addDice(lblDice2, 70, 20)
addDice(lblDice3, 130, 20)
addDice(lblDice4, 190, 20)
addDice(lblDice5, 250, 20)
resultscount(lblYatzee, "Yahtzees: 0", 20, 140)
resultscount(lblFourOfAKind, "Four of a Kinds: 0", 20, 180)
resultscount(lblThreeOfAKind, "Three of a Kinds: 0", 20, 220)
AddButton(butRoll, "Roll", 100, 90)
End Sub
Private Sub resultscount(ByRef results As TextBox, ByRef Name As String, ByVal x As Integer, ByVal y As Integer)
results.Text = Name
results.Location = New Point(x, y)
results.Width = 150
Me.Controls.Add(results)
End Sub
Private Sub RollDice() Handles butRoll.Click
Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0}
RollSingleDice(lblDice1)
RollSingleDice(lblDice2)
RollSingleDice(lblDice3)
RollSingleDice(lblDice4)
RollSingleDice(lblDice5)
GetStats()
lblYatzee.Text = "Yatzees: " & nYatzee
lblFourOfAKind.Text = "Four Of A Kind: " & nFourOfAKind
lblThreeOfAKind.Text = "Three Of A Kind: " & nThreeOfAKind
End Sub
Private Sub AddButton(ByRef results As Button, ByRef Name As String, ByVal x As Integer, ByVal y As Integer)
results.Text = Name
results.Location = New Point(x, y)
results.Width = 150
Me.Controls.Add(results)
End Sub
Private Sub RollSingleDice(ByRef lblDice As Label)
lblDice.Text = rnd.Next(1, 7)
End Sub
Private Sub GetStats()
Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0}
For Each lbl As Label In Me.Controls.OfType(Of Label)()
arrNumbers(lbl.Text - 1) += 1
Next
For Each i As Integer In arrNumbers
If i = 5 Then
nYatzee += 1
ElseIf i = 4 Then
nFourOfAKind += 1
ElseIf i = 3 Then
nThreeOfAKind += 1
End If
Next
End Sub
End Class
Showing posts with label methods. Show all posts
Showing posts with label methods. Show all posts
Wednesday, May 11, 2011
VB Practice 7 - Methods II - Yahtzee
VB Practice 6 - Methods
Public Class Form1
Dim chara, word, sentVal, space As Integer
Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
'nchar = GetCharCount(TextBox1.Text)
chara = TextBox1.Text.Length.ToString
word = getWordCount(TextBox1.Text)
space = getSpacesCount(TextBox1.Text)
sentVal = getSentenceCount(TextBox1.Text)
updatestats()
End Sub
Private Function getWordCount(ByVal value As String) As Integer
Dim word As String
word = System.Text.RegularExpressions.Regex.Matches(value, "\S+").Count
Return word
End Function
Private Function getSentenceCount(ByVal value As String) As Integer
Dim sentVal As String = 1
sentVal = System.Text.RegularExpressions.Regex.Matches(value, "\.").Count
sentVal += System.Text.RegularExpressions.Regex.Matches(value, "\!").Count
sentVal += System.Text.RegularExpressions.Regex.Matches(value, "\?").Count
Return sentVal
End Function
Private Function getSpacesCount(ByVal value As String) As Integer
For Each s As String In value
If s = " " Then
space += 1
End If
Next
Return space
End Function
Private Sub updatestats()
txtChara.Text = chara
txtword.Text = word
txtspaces.Text = space
txtSent.Text = sentVal
End Sub
End Class
Monday, April 4, 2011
Visual Basic Methods
The following is an example of how to use a sub and a function in order to reuse code.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str1 As String = TextBox1.Text
Dim strFormatted As String = ""
strFormatted = digitsOnly(str1)
checkNumberLength(strFormatted)
TextBox1.Text = strFormatted
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strFormatted As String = "("
TextBox1.Text = digitsOnly(TextBox1.Text)
checkNumberLength(TextBox1.Text)
Dim x As Integer = 1
For Each c As String In TextBox1.Text
If x = 4 Then
strFormatted += ") "
End If
If x = 7 Then
strFormatted += "-"
End If
strFormatted += c
x += 1
Next
TextBox1.Text = strFormatted
End Sub
Private Function digitsOnly(ByVal str As String) As String
Dim strFormatted As String = ""
For Each c As String In str
If Char.IsDigit(c) Then
strFormatted += c
End If
Next
Return strFormatted
End Function
Private Sub checkNumberLength(ByVal num As String)
If num.Length <> 10 Then
MsgBox("Sorry, you need 10 numbers")
End If
End Sub
End Class
Subscribe to:
Posts (Atom)