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
  

Methods

Here is how Methods are done using different Functions .

  Public Class Form1
  
 Public Class Form1
  
   Dim nChar As Integer
  
   Dim nWord As Integer
  
   Dim nSpace As Integer
  
   Dim nSentence As Integer
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Dim c As String = txtPhrase.Text
  
     nChar = textLength(c)
  
     nWord = CountWords(c)
  
     nSentence = sentenceCount(c)
  
     nSpace = wordSpace(c)
  
     updateChar()
  
   End Sub
  
   Private Sub updateChar()
  
     TextBox1.Text = nChar
  
     TextBox2.Text = nWord
  
     TextBox3.Text = nSentence
  
     TextBox4.Text = nSpace
  
   End Sub
  
   Private Function textLength(ByVal str As String) As String
  
     Return str.Length
  
   End Function
  
   Public Function CountWords(ByVal value As String) As Integer
  
     Return value.Split(" ").Count()
  
   End Function
  
   Public Function wordSpace(ByVal space As String) As String
  
     Dim y As Integer
  
     y = space.Split(" ").Count
  
     y -= 1
  
     Return y
  
   End Function
  
   Public Function sentenceCount(ByVal sentence As String) As String
  
     Dim x As Integer
  
     x = sentence.Split(".").Count()
  
     x -= 1
  
     Return x
  
   End Function
  
 End Class  

Sunday, May 8, 2011

Programmatic adding of elements

 This code will create a times table..

  
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     For x = 1 To 10
  
       For y = 1 To 10
  
         Dim button1 As New Button
  
         button1.Location = New Point(40 * x, 40 * y)
  
         button1.Width = 40
  
         Me.Controls.Add(button1)
  
         button1.Text = x * y
  
       Next
  
     Next
  
   End Sub  

Saturday, May 7, 2011

Values and Formulas

In this blog i will show how to problem solve :
1. morgan collecting stuffed animals prooblem
  • set variables for the animals being counted
    • cows(c) sheep(s) and total(t)
  • determine a formula for solving the problem
    • c + s= t
  • Morgan has 6 cows and 7 sheep so
    • 6+7= 13
2. Mega burgers
  • burgers * price
    • 4*7= 28
3. Fruit question
  • dim apples as double = 15
  • dim oranges as double = 12
4. Horse problem
  • dim horse as double = 33
5. apples to oranges problem
  • dim apples, oranges as double
  • apples= 15
  • oranges = apples*3
6. Grade subject problem
dim score ,math, science,eng, geo as decimal
dim score as double =
dim total as decimal
  •  math+ science = 25
  • score - total= 7
  • total /4 = 14

Conditionals

Problem # 1 :  Here i am gonna demonstrate how to make a shipping calculator:

  Dim shipping As Decimal = 50
  
   Dim price As Decimal
  
   Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
  
     price = shipping + txtPurchasePrice.Text
  
     If price > 50 Then
  
       shipping = 0
  
     End If
  
     MsgBox(("Your Total Price is: " & FormatCurrency(price)))
  
   End Sub
  
   Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  
     txtPurchasePrice.Text = ""
  
   End Sub  


This is an example of a program that will control the heater and AC

  Dim temperature As Double
  
   Dim Heat, AC As Double
  
   Private Sub btnTempControl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTempControl.Click
  
     temperature = txtCalcTemp.Text
  
     If temperature < 72 Then
  
       MsgBox("The Heater is being activated ")
  
     Else
  
       If temperature > 76 Then
  
         MsgBox("The AC is being activated")
  
       Else
  
         MsgBox("the system is idle")
  
       End If
  
     End If
  
   End Sub  

 Here i am going to demonstrate a clothing size calculator:

 Dim age As Double
  
   Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
  
     age = txtAgeInput.Text
  
     If age <= 2 Then
  
       MsgBox("you should go with XS")
  
     Else
  
       If age <= 4 Then
  
         MsgBox("Small")
  
       Else
  
         If age <= 8 Then
  
           MsgBox("Medium is the way to go")
  
         Else
  
           If age <= 12 Then
  
             MsgBox("Large")
  
           Else
  
             If age > 13 Then
  
               MsgBox("XL")
  
             End If
  
           End If
  
       End If
  
     End If
  
     End If
  

Friday, April 1, 2011

Array Project (days of the week)

Days of the week Project


 Dim week As New Hashtable 'declare at class leve
  
 Dim day As DictionaryEntry
  
 week.Add("Sunday", "Day 1")
  
 week.Add(Monday", "Day 2")"
  
 week.Add(Tuesday", "Day 3")"
  
 week.Add(Wednesday", "Day 4")"
  
 week.Add(Thursday", "Day 5")"
  
 week.Add("Friday", "Day 6")"
  
 week.add("Saturday", "day 7")'Display a single Item
  
 MsgBox(day.Key)For Each day In week
  
 Next
  
 txtTitle.Text = ""
  

Array Project *revised

 Here is an example of a Printer Queue:
  Dim printJobs As New Queue
  
   Private Sub btnSendJob_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendJob.Click
  
     Dim printer As New Hashtable
  
     printer.Add("Title", (txtTitle.Text))
  
     printer.Add("Pages", (cbPages.Text))
  
     printJobs.Enqueue(printer)
  
     lstJobs.Items.Clear()
  
     For Each h As Hashtable In
  
      printJobs
  
       lstJobs.Items.Add(h.Item("Title") & " " & (h.Item("Pages")))
  
     Next
  
     txtTitle.Text = ""
  
     Me.cbPages.Items.Remove("")
  
   End Sub
  
   Private Sub btnClearCurrent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearCurrent.Click
  
     lstJobs.Items.Clear()
  
   End Sub
  
 End Class
  

 In this example i am going to demonstrate how to create an array that will  store  users; Name(first and last), and Email address.
  Dim Names As New Hashtable
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     Names(1) = (txtFirstName.Text)
  
     Names(2) = (txtLastName.Text)
  
     Names(3) = (txtEmail.Text)
  
     txtEmail.Clear()
  
     txtFirstName.Clear()
  
     txtLastName.Clear()
  
   End Sub
  
   Private Sub btnFirstName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirstName.Click
  
     MsgBox(Names(1))
  
   End Sub
  
   Private Sub btnLastName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastName.Click
  
     MsgBox(Names(2))
  
   End Sub
  
   Private Sub btnEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmail.Click
  
     MsgBox(Names(3))
  
   End Sub
  
 End Class