Wednesday, April 20, 2011

Practice 7 II

 Public Class Form1
  
   'Declaring variables for the form
  
   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)
  
     'Generic code for all labels (Dice Roll)
  
     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 addCounts(ByRef ctxt As TextBox, ByVal c As Integer, ByVal d As Integer, ByVal e As String)
  
     'Generic code for all textboxes (Counters)
  
     ctxt.Text = e
  
     ctxt.Location = New Point(c, d)
  
     ctxt.Width = 150
  
     Me.Controls.Add(ctxt)
  
   End Sub
  
   Private Sub addRoll(ByRef butr As Button, ByVal s As String, ByVal t As Integer, ByVal u As Integer)
  
     'Generic code for all buttons (Button)
  
     butRoll.Text = s
  
     butRoll.Location = New Point(t, u)
  
     Me.Controls.Add(butRoll)
  
   End Sub
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Adding the Dice Rolls to the form
  
     addDice(lblDice1, 10, 20)
  
     addDice(lblDice2, 70, 20)
  
     addDice(lblDice3, 130, 20)
  
     addDice(lblDice4, 190, 20)
  
     addDice(lblDice5, 250, 20)
  
     'Adding the Counters to the form
  
     addCounts(lblYatzee, 20, 140, "Yatzees: 0")
  
     addCounts(lblFourOfAKind, 20, 180, "Four Of A Kind: 0")
  
     addCounts(lblThreeOfAKind, 20, 220, "Three Of A Kind: 0")
  
     'Adding the Buttons to the form
  
     addRoll(butRoll, "Roll", 100, 90)
  
   End Sub
  
   Private Sub codeDice(ByRef codelbl As Label)
  
     'Generic code for the dice
  
     codelbl.Text = rnd.Next(1, 7)
  
   End Sub
  
   Private Sub getTotal()
  
     'Declare the variable used to find the total
  
     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
  
     'Totals up three of a kind, four of a kind, and yatzee.
  
     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 nameLabel()
  
     'Display the total for yatzees, four of a kind, and three of a kind
  
     lblYatzee.Text = "Yatzees: " & nYatzee
  
     lblFourOfAKind.Text = "Four Of A Kind: " & nFourOfAKind
  
     lblThreeOfAKind.Text = "Three Of A Kind: " & nThreeOfAKind
  
   End Sub
  
   Private Sub RollDice() Handles butRoll.Click
  
     'Call the dice roll to the form
  
     codeDice(lblDice1)
  
     codeDice(lblDice2)
  
     codeDice(lblDice3)
  
     codeDice(lblDice4)
  
     codeDice(lblDice5)
  
     'Call the total to the form
  
     getTotal()
  
     'Call the display to the form
  
     nameLabel()
  
   End Sub
  
 End Class  

Sunday, April 17, 2011

Practice 4

1.) Put a textbox, listbox and a button on the form. The user must enter a number and press the button. When the button is pressed, it will add that many items to the listbox.
 Public Class Form1
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim adds As Double
  
     Dim int, num As Integer
  
     adds = CDbl(TextBox1.Text)
  
     num = 0
  
     For int = 1 To adds
  
       num = num + 1    
  
       ListBox1.Items.Add(num)
  
     Next
  
   End Sub
  
 End Class  

2.)If you were to put away a certain amount of money every month, how many years would it take you to save up $10,000. Use a textbox to gather the amount that is to be put away every month. Use a label to display the number of years it would take.
 Public Class Form1
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim intPay As Integer = CInt(TextBox1.Text)
  
     Dim intGoal As Integer = 10000
  
     Dim intMonths, intTotal As Integer
  
     Dim decYears As Decimal
  
     intPay = CInt(TextBox1.Text)
  
     intGoal = 10000
  
     For intTotal = 1 To intGoal
  
       intTotal += intPay
  
       intMonths = intMonths + 1
  
       decYears = intMonths / 12
  
       Label1.Text = (decYears.ToString & " years")
  
     Next
  
   End Sub
  
 End Class  

3.) Write a program that will create a times table.

1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20 …..
10 20 30 40…..

You could use labels or buttons to hold the numbers. Generate these dynamically and add them to the Me.Controls collection.

You will have to use two loops, one inside of the other to get this to work.
 Public Class Form1
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim c, d As Integer
  
     c = 1
  
     Button1.Visible = False
  
     For c = 1 To 10
  
       For d = 1 To 10
  
         Dim multibutton As New Button
  
         multibutton.Location = New Point(70 * c, 30 * d)
  
         multibutton.Width = 40
  
         multibutton.Text = c * d
  
         Me.Controls.Add(multibutton)
  
       Next
  
     Next
  
   End Sub
  
 End Class  

Blog: Loops

1.) Do (While First) Loop
 Module Module1
  
   Sub Main()
  
      ' Locals used in Do While loop.
  
      Dim i As Integer = 100
  
      Dim z As Integer = 0
  
      ' Loop.
  
      Do While i >= 0 And z <= 20
  
        Console.WriteLine("i = {0}, z = {1}", i, z)
  
        i = i - 10
  
        z = z + 3
  
      Loop
  
   End Sub
  
 End Module
  

2.) Do (While Last) Loop
 Public Class MainClass
  
   Shared Sub Main(ByVal args As String())
  
     Dim counterVariable As Integer = 100
  
     Do
  
      Console.WriteLine("counterVariable: {0}", counterVariable)
  
      counterVariable = counterVariable + 1
  
     Loop While counterVariable < 10
  
   End Sub
  
 End Class
  

3.) For Loop
 Module Module1
  
   Sub Main()
  
      ' This loop goes from 0 to 5.
  
      For value As Integer = 0 To 5
  
        ' Exit condition if the value is three.
  
        If (value = 3) Then
  
           Exit For
  
        End If
  
        Console.WriteLine(value)
  
      Next
  
   End Sub
  
 End Module
  

4.) For Each Loop
 Module Module1
  
   Sub Main()
  
      ' The input array.
  
      Dim ferns() As String = {"Psilotopsida", _
  
                      "Equisetopsida", _
  
                      "Marattiopsida", _
  
                      "Polypodiopsida"}
  
      ' Loop over each element with For Each.
  
      For Each fern As String In ferns
  
        Console.WriteLine(fern)
  
      Next
  
   End Sub
  
 End Module
  

Friday, April 8, 2011

Practice 6 Project

 Public Class Form1
  
   Dim C As Integer
  
   Dim W As Integer
  
   Dim Se As Integer
  
   Dim Sp As Integer
  
   Public Function getWord(ByVal InputString As String) As String
  
     Return Split(System.Text.RegularExpressions.Regex.Replace(InputString, "\s+", Space(1))).Length
  
   End Function
  
   Public Function getSentence(ByVal InputString As String) As Integer
  
     Dim periods As Integer
  
     Dim questionmarks As Integer
  
     Dim exclamations As Integer
  
     periods = InputString.Split(".").Length
  
     questionmarks = InputString.Split("?").Length
  
     exclamations = InputString.Split("!").Length
  
     Return periods + questionmarks + exclamations
  
   End Function
  
   Public Function getSpaces(ByVal sText As String) As Long
  
     Dim Spaces As Integer = 0
  
     For Each s As String In sText
  
       If s = " " Then
  
         Spaces += 1
  
       End If
  
     Next
  
     Return Spaces
  
   End Function
  
   Public Function CountCharacters(ByVal sText As String) As Integer
  
     Return TextBox1.Text.Length.ToString
  
   End Function
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     'Count characters
  
     C = CountCharacters(TextBox1.Text)
  
     'Count Sentences
  
     Se = getSentence(TextBox1.Text)
  
     'Count Word
  
     W = getWord(TextBox1.Text)
  
     'Count Spaces
  
     Sp = getSpaces(TextBox1.Text)
  
     UpdateTextBox()
  
   End Sub
  
   Private Sub UpdateTextBox()
  
     'Count characters
  
     TextBox2.Text = TextBox1.Text.Length.ToString
  
     TextBox2.Text = C
  
     'Count Sentences
  
     TextBox3.Text = getSentence(TextBox1.Text)
  
     TextBox3.Text = Se
  
     'Count Word
  
     TextBox4.Text = getWord(TextBox1.Text)
  
     TextBox4.Text = W
  
     'Count Spaces
  
     TextBox5.Text = getSpaces(TextBox1.Text)
  
     TextBox5.Text = Sp
  
   End Sub
  
 End Class