Monday, May 9, 2011

Methods #2

In this post i will demonstrate how to create functions for different buttons to condense cluttered code.

 Public Class Form1
  
   Dim lblDice1, lblDice2, lblDice3, lblDice4, lblDice5 As New Label ' Declares each new label
  
   Dim WithEvents butRoll As New Button 'declares each new button
  
   Dim nYatzee, nFourOfAKind, nThreeOfAKind As New Integer ' sets each hand as an 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 'Sets the text of each label
  
     lbl.Location = New Point(x, y) 'vasiably sets the location of each label
  
     lbl.Font = New Drawing.Font("Microsoft Sans Serif", 28.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
  
     lbl.Height = 40 'Sets the height
  
     lbl.Width = 40
  
     Me.Controls.Add(lbl) ' Adds each button to the form controls
  
   End Sub
  
   Private Sub points(ByRef txt As TextBox, ByVal x As Integer, ByVal y As Integer, ByRef c As String)
  
     txt.Text = c
  
     txt.Location = New Point(x, y) ' Sets the locations variably
  
     txt.Width = 150
  
     Me.Controls.Add(txt) ' Adds the textbox to the controls
  
   End Sub
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     addDice(lblDice1, 10, 20) ' Sets the location 
  
     addDice(lblDice2, 70, 20)
  
     addDice(lblDice3, 130, 20)
  
     addDice(lblDice4, 190, 20)
  
     addDice(lblDice5, 250, 20)
  
     points(lblYatzee, 20, 140, "Yatzees: 0") ' Sets the text in each textbox
  
     points(lblFourOfAKind, 20, 180, "Four Of A Kind: 0")
  
     points(lblThreeOfAKind, 20, 220, "Three Of A Kind: 0")
  
     butRoll.Text = "Roll" 'Sets the roll text
  
     butRoll.Location = New Point(100, 90) 'New location of butRoll
  
     Me.Controls.Add(butRoll)
  
   End Sub
  
   Private Sub RollDice() Handles butRoll.Click
  
     Roll(lblDice1) 'Rolls the dice 
  
     Roll(lblDice2)
  
     Roll(lblDice3)
  
     Roll(lblDice4)
  
     Roll(lblDice5)
  
     getstats() ' retrieves stats from the textboxes and labels
  
     updatetext() ' updates the stats of each text box 
  
   End Sub
  
   Private Sub Roll(ByRef lbl As Label)
  
     lbl.Text = rnd.Next(1, 7) ' makes each label show a random number 
  
   End Sub
  
   Private Sub getstats()
  
     Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0} ' Gets stats function
  
     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
  
   Private Sub updatetext() 'updates the textboxes
  
     lblYatzee.Text = "Yatzees: " & nYatzee
  
     lblFourOfAKind.Text = "Four Of A Kind: " & nFourOfAKind
  
     lblThreeOfAKind.Text = "Three Of A Kind: " & nThreeOfAKind
  
   End Sub
  
 End Class
  

No comments:

Post a Comment