Tuesday, May 10, 2011

Methods 2.0

Grab the code below and try to arrange the code into logical groups of code.  Here are some pointers:
1. Create a single method that will be responsible for creating the place holder for each of the dice numbers.
 a. You will likely need to pass the label in as an attribute in the method call
2. Create a single method that will be responsible for adding the button and 3 stats text boxes
3. Add a method who's job is just to roll the dice.
4. Add a method who's job is to collect the stats of how many there are of each number
5. Add a method who's job is to update the stats textboxes
6. Add comments to the code blocks to explain what is going on.

 Public Class Form1
  
   'Declare all variables
  
   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)
  
     'Code labels for 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)
  
     'Code for 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)
  
     'Button coding
  
     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
  
     'Dice rolls
  
     addDice(lblDice1, 10, 20)
  
     addDice(lblDice2, 70, 20)
  
     addDice(lblDice3, 130, 20)
  
     addDice(lblDice4, 190, 20)
  
     addDice(lblDice5, 250, 20)
  
     'Adding counters
  
     addCounts(lblYatzee, 20, 140, "Yatzees: 0")
  
     addCounts(lblFourOfAKind, 20, 180, "Four Of A Kind: 0")
  
     addCounts(lblThreeOfAKind, 20, 220, "Three Of A Kind: 0")
  
     'Add button
  
     addRoll(butRoll, "Roll", 100, 90)
  
   End Sub
  
   Private Sub codeDice(ByRef codelbl As Label)
  
     'Dice code
  
     codelbl.Text = rnd.Next(1, 7)
  
   End Sub
  
   Private Sub getTotal()
  
     'Declare variable for 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
  
     'Completes 3-of-a-kind, 4-of-a-king, and yahtzee!
  
     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()
  
     'Displays total for 3-of-a-kind, 4-of-a-king, and yahtzee
  
     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 dice roll to form
  
     codeDice(lblDice1)
  
     codeDice(lblDice2)
  
     codeDice(lblDice3)
  
     codeDice(lblDice4)
  
     codeDice(lblDice5)
  
     'Call total to form
  
     getTotal()
  
     'Call display to form
  
     nameLabel()
  
   End Sub
  
 End Class  

Practice 6: Methods

Create the program
that you see on the right. The program
will allow long text to inputted into it through a textbox. When the user clicks "Analyze", it
will do 4 things:
  1. Count the total characters
  2. Count the words
  3. Count the sentences
  1. Count the spaces

I want each of these
counts to be calculated in their own functions. Name these functions according to what is happening inside of it.

You should also have
a sub who's job is to update the text boxes with the proper counts.

You will have to do
some googling to figure out how to do word and sentence counts. Spaces and total chars should be pretty
straight forward.

 Public Class Form1
  
   Public Function SentenceSetup(ByVal InputString As String) As Integer
  
     Dim period, qm, ex As Integer
  
     period = InputString.Split(".").Length
  
     qm = InputString.Split("?").Length
  
     ex = InputString.Split("!").Length
  
     Return (period + qm + ex) / 2
  
   End Function
  
   Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
  
     'Count characters
  
     txtChar.Text = AnalyzeText.Text.Length.ToString
  
     'Count words
  
     Dim str As String = AnalyzeText.Text
  
     Dim strA() As String = str.Split(" ")
  
     Dim TChrs As Int32
  
     Dim i As Int32
  
     For i = strA.Length - 1 To 0 Step -1
  
       TChrs += strA(i).Length
  
     Next
  
     txtWords.Text = strA.Length
  
     'Count spaces
  
     Dim Spaces As Integer = 0
  
     For Each s As String In AnalyzeText.Text
  
       If s = " " Then
  
         Spaces += 1
  
       End If
  
     Next
  
     txtSpace.Text = Spaces
  
     'Count Sentences
  
     txtSentence.Text = SentenceSetup(AnalyzeText.Text)
  
   End Sub
  
 End Class  

Arrays Assignment

1. Create a string array that
will store the days of the week. The array should fill when the form loads. Add a button (you can drag it on the
form). When the button is clicked,
loop through the array and pop up a message box for each day of the
week.

  Private Sub Button1_Click(ByVal sender As System.Object,
  
  ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim week As New Hashtable
  
     Dim daysofweek As DictionaryEntry
  
     week.Add("1", "Sunday")
  
     week.Add("2", "Monday")
  
     week.Add("3", "Tuesday")
  
     week.Add("4", "Wednesday")
  
     week.Add("5", "Thursday")
  
     week.Add("6", "Friday")
  
     week.Add("7", "Saturday")
  
     For Each daysofweek In week
  
       MsgBox("" & daysofweek.Value)
  
     Next
  
   End Sub
  
 End Class  


2. Create a form with textboxes
for the following:
 - first name
 - last name
 - email.
Add labels to describe each box.
Add a button for adding the user. The add user button click event will do the following:
 - add the user information
into a hashtable
 - clear the text from the
text boxes

We want to add the information into the hashtable using three keys:
FirstName, LastName, Email.

Add First Name, Last Name, and Email buttons to the bottom of the
form. Each of these buttons is
going to fire off a message box that will show the information that was
just added in.


 Public Class Form1
  
   Dim adduser As New Hashtable
  
   Private Sub btFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btFirst.Click
  
     MessageBox.Show(txtFirst.Text)
  
   End Sub
  
   Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
  
     MessageBox.Show(txtLast.Text)
  
   End Sub
  
   Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click
  
     MessageBox.Show(txtEmail.Text)
  
   End Sub
  
   Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  
     adduser.Clear()
  
     adduser.Add("txtFirst", txtFirst.Text)
  
     adduser.Add("txtLast", txtLast.Text)
  
     adduser.Add("txtEmail", txtEmail.Text)
  
     txtFirst.Clear()
  
     txtLast.Clear()
  
     txtEmail.Clear()
  
   End Sub
  
 End Class  



  1. Create a form that will work
    as a printer queue. This form will
    have the following inputs:
    1. Title of the
      document
    2. Pages to be printed

When
submitted the click event will do the following:
  1. Add the title and pages to a
    hashtable
  2. Add the hashtable to a queue
    which is a class variable
  3. Clear the title and set the
    pages back to one
  4. Refresh the queue list.

The
Clear Current button will:
  1.  remove the next item in the queue
  2. Refresh the queue list
    box 
 Public Class Printer
  
   Dim jobs As New Queue
  
   Private Sub btnSendJob_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendJob.Click
  
     Dim qlist As New Hashtable
  
     jobs.Enqueue(qlist)
  
     qlist.Add("Title", txtTitle.Text)
  
     qlist.Add("Pages", NumericUpDown1.Value)
  
     refreshalljobs()
  
     NumericUpDown1.Value = 1
  
     txtTitle.Text = ""
  
   End Sub
  
   Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  
     If jobs.Count > 0 Then
  
       jobs.Dequeue()
  
     End If
  
     refreshalljobs()
  
   End Sub
  
   Private Sub refreshalljobs()
  
     qlist.Items.Clear()
  
     For Each hash As Hashtable In jobs
  
       qlist.Items.Add(hash.Item("Title") & "(" & hash.Item("Pages") & ")")
  
     Next
  
   End Sub
  
 End Class  

Saturday, April 30, 2011

Arrays

In this example, we create two string arrays in the VB.NET language. The first array is created with an initialization statement; you use curly brackets on the right side and can specify each string element. You do not need to specify the size of the array on the left side. The second array uses the longer syntax: you must specify the capacity of the array in the Dim statement. Then, you can assign various elements into the memory you allocated.
 Sub Main()
  
      ' Create an array with the simple initialization syntax.
  
      Dim array() As String = {"dog", "cat", "fish"}
  
      ' Loop over the array.
  
      For Each value As String In array
  
        Console.WriteLine(value)
  
      Next
  
      ' Pass array as argument.
  
      M(array)
  
      ' Create an array in several statements.
  
      ' ... Use the maximum index in the declaration.
  
      ' ... Begin indexing at zero.
  
      Dim array2(2) As String
  
      array2(0) = "bird"
  
      array2(1) = "dog"
  
      array2(2) = "gopher"
  
      ' Loop.
  
      For Each value As String In array2
  
        Console.WriteLine(value)
  
      Next
  
      ' Pass as argument.
  
      M(array2)
  
   End Sub
  
   Sub M(ByRef array() As String)
  
      ' Write length.
  
      Console.WriteLine(array.Length)
  
   End Sub
  

The following source code shows all important operations in a HashTable:

 Public Class Form1
  
   Private Sub Button1_Click(ByVal sender As System.Object, 
  
      ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim weeks As New Hashtable
  
     Dim day As DictionaryEntry
  
     weeks.Add("1", "Sun")
  
     weeks.Add("2", "Mon")
  
     weeks.Add("3", "Tue")
  
     weeks.Add("4", "Wed")
  
     weeks.Add("5", "Thu")
  
     weeks.Add("6", "Fri")
  
     weeks.Add("7", "Sat")
  
     'Display a single Item
  
     MsgBox(weeks.Item("5"))
  
     'Search an Item
  
     If weeks.ContainsValue("Tue") Then
  
       MsgBox("Find")
  
     Else
  
       MsgBox("Not find")
  
     End If
  
     'remove an Item
  
     weeks.Remove("3")
  
     'Display all key value pairs
  
     For Each day In weeks
  
       MsgBox(day.Key " -- " day.Value)
  
     Next
  
   End Sub
  
 End Class
To add an object to a queue, the Enqueue method is invoked. There is only constructor for this method, with a single argument: the object to be added to the Queue. Any object can be added to an instance of the Queue class - from basic objects such as integers and strings to other classes and even other Queue classes. It is also possible to add null values using the VB.NET Nothing keyword or the System.DBNull.Value class. Each Queue can contain many different types of objects. The code below shows a variety of objects added to an instance of a Queue class:
 Dim MyQueue As New Queue(4)
  
 Dim MyOtherQueue As New Queue
  
 Dim MyNumber As Integer = 24
  
 Dim MyText As String = "ASP Documentation Tool"
  
 MyQueue.Enqueue(MyNumber)
  
 MyQueue.Enqueue(MyText)
  
 MyQueue.Enqueue(MyOtherQueue)
  
 MyQueue.Enqueue(System.DBNull.Value)

Sunday, April 24, 2011

Loops

The following program, in which the condition in the Do loop is "num <=7", displays the numbers from 1 through 7. (After the Do loop terminates, the value of num will be 8.)

 Public Class Form1
  
   Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPurchase.Click
  
     'Display the number from 1 to 7
  
     Dim num As Integer = 1
  
     Do While num <= 7
  
       lstNumbers.Items.Add(num)
  
       num += 1  'add 1 to the value of num
  
     Loop
  
   End Sub
  
 End Class  

While loop keeps executing until the condition against which it tests remain true. The syntax of while loop looks like this:
    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPurchase.Click
  
 Sub Main()
  
 Dim d, e As Integer
  
 d = 0
  
 e = 6
  
 While e > 4
  
 e -= 1
  
 d += 1
  
 End While
  
 System.Console.WriteLine("The Loop ran " & e & "times")
  
 End Sub
  
 End Class  

Monday, March 7, 2011

Practice #4 - Loops

1. Put a textbox 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 buttons to the form.
   Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim howmany As Integer = TextBox1.Text
  
     Dim x As Integer = 0
  
     ListBox1.Items.Clear()
  
     For x = 1 To howmany
  
       ListBox1.Items.Add(x & " item")
  
     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 os years it would take.
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim pay, months, total, goal As Integer
  
     Dim years As Decimal
  
     pay = CInt(txtPay.Text)
  
     goal = 10000
  
     For total = 1 To goal
  
       total += pay
  
       months = months + 1
  
       years = months / 12
  
       lblThree.Text = ("It will take " & years.ToString("f1") & " years to make the $10,000 goal.")
  
     Next
  
   End Sub
  
 End Class  

3. Write a program that creates a times table.
   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 ofthe other to get this to work.
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim x, y As Integer 
  
     x = 1
  
     Button1.Visible = False
  
     For x = 1 To 10
  
       For y = 1 To 10
  
         Dim newbutton As New Button
  
         newbutton.Location = New Point(50 * x, 50 * y)
  
         newbutton.Width = 40
  
         newbutton.Text = x * y
  
         Me.Controls.Add(newbutton)
  
       Next
  
     Next
  
   End Sub
  
 End Class
  

Wednesday, February 16, 2011

Practice #3 - Form Objects

1. Create a form witha single button (use the gui for this one). When the button is hovered over, moe the button to another part of the form. The button should move back and forth between 2 points.
   Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
  
     Dim x As New Point(10, 10)
  
     Dim y As New Point(50, 50)
  
     If Button1.Location = x Then
  
       Button1.Location = y
  
     Else
  
       Button1.Location = x
  
     End If
  
   End Sub
  
 End Class  

2. Create a form with 4 buttons and one text box (use the gui).  The botton wording should describe what it does.
a. Button 1: This button will make the textbox enabled/disabled.
b. Button 2: Turn a background color on the textbox on and off. (Toggle between white and another color)
c. Button 3: Put the text inside of the text box and take it away
d. Button 4: Change the border style of the textbox between none and fixed3D.
 Private Sub btnTxtEnDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTxtEnDisable.Click
  
 If txtBox.Enabled = True Then
  
 txtBox.Enabled = False
  
 End If
  
 txtBox.Enabled = True
  
 End Sub
  
   Private Sub btnTxtBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTxtBackColor.Click
  
     If txtBox.BackColor = Color.Blue Then
  
       txtBox.BackColor = Color.White
  
     Else
  
       txtBox.BackColor = Color.Blue
  
     End If
  
   End Sub  

   Private Sub btnTxtErase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTxtErase.Click
  
     If txtBox.Text = "Teeps is the man" Then
  
       txtBox.Text = ""
  
     Else
  
       txtBox.Text = "Teeps is the man"
  
     End If
  
   End Sub  

   Private Sub btnTxtBorderStyle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTxtBorderStyle.Click
  
     If txtBox.BorderStyle = BorderStyle.None Then
  
       txtBox.BorderStyle = BorderStyle.Fixed3D
  
     Else
  
       txtBox.BorderStyle = BorderStyle.None
  
     End If
  
   End Sub
  
 End Class  

3. Create a form with a button (using the gui). When this button is clicked, it will create 3 labels and 3 textboxes associated with those labels.  When the textboxes are hovered over, change their background color.  When is not being hovered over, change the background color back to white.
 Public Class Form1
  
   Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     txtFirst.Show()
  
     txtSecond.Show()
  
     txtThird.Show()
  
     lblFirst.Show()
  
     lblSecond.Show()
  
     lblThird.Show()
  
   End Sub
  
   Private Sub txtFirst_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFirst.MouseEnter
  
     txtFirst.BackColor = Color.Red
  
   End Sub
  
   Private Sub txtFirst_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFirst.MouseLeave
  
     txtFirst.BackColor = Color.White
  
   End Sub
  
   Private Sub txtSecond_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSecond.MouseEnter
  
     txtSecond.BackColor = Color.Yellow
  
   End Sub
  
   Private Sub txtSecond_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSecond.MouseLeave
  
     txtSecond.BackColor = Color.White
  
   End Sub
  
   Private Sub txtThird_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtThird.MouseEnter
  
     txtThird.BackColor = Color.Green
  
   End Sub
  
   Private Sub txtThird_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtThird.MouseLeave
  
     txtThird.BackColor = Color.White
  
   End Sub
  
 End Class  
 
4. Create a form with 3 buttons and a listbox. Load 3 items on load so that it's not empty
Button1: Select Item 1
Button 2: Select Item 2
Button 3: Select Item 3
 Public Class Form1
  
   Dim str1, str2, str3 As String
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     str1 = "First is the worst"
  
     str2 = "Second is the best"
  
     str3 = "Third is.....?"
  
     ListBox1.Items.Add(str1)
  
     ListBox1.Items.Add(str2)
  
     ListBox1.Items.Add(str3)
  
   End Sub
  
   Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     ListBox1.SelectedIndex = 0
  
   End Sub
  
   Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  
     ListBox1.SelectedIndex = 1
  
   End Sub
  
   Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
  
     ListBox1.SelectedIndex = 2
  
   End Sub
  
 End Class