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  

Monday, March 28, 2011

Practice 3

1.Create a form with a single
button (use the gui for this one). When the button is hovered over, move the button to another part of
the form. The button should move back and forth between 2 points.

If you want to be special, move the button to a random part of the
form. You will have to use a random
number generator for the x and y coordinates making sure the random x and
y are not larger than the form height and width.
 Public Class Form1
  
   Dim pt1 As New Point(300, 100)
  
   Dim pt2 As New Point(90, 12)
  
   Dim pt3 As New Point(600, 200)
  
   Private Sub Button2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button2.MouseMove
  
     If Button2.Location = pt1 Then
  
       Button2.Location = pt2
  
     ElseIf Button2.Location = pt2 Then
  
       Button2.Location = pt3
  
     ElseIf Button2.Location = pt3 Then
  
       Button2.Location = pt1
  
     End If
  
   End Sub
  
   Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
  
     Dim rdmLocation As New Random
  
     Dim x As Integer = rdmLocation.Next(0, Me.Width)
  
     Dim y As Integer = rdmLocation.Next(0, Me.Height)
  
     Button1.Location = New Point(x, y)
  
   End Sub
  
   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
     Button2.Location = pt1
  
   End Sub
  
 End Class  

2.Create a form with 4 buttons
and one text box (use the gui). The
button 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 text inside of the text box and take it away
d.Button 4: Change the border style of the textbox between none and fixed3d

 Public Class Form1
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     If TextBox1.Enabled = True Then
  
       TextBox1.Enabled = False
  
     Else
  
       TextBox1.Enabled = True
  
     End If
  
   End Sub
  
   Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  
     If TextBox1.BackColor = Color.White Then
  
       TextBox1.BackColor = Color.Blue
  
     Else
  
       TextBox1.BackColor = Color.White
  
     End If
  
   End Sub
  
   Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
  
     If TextBox1.Text = "" Then
  
       TextBox1.Text = "I'm here"
  
     Else
  
       TextBox1.Text = ""
  
     End If
  
   End Sub
  
   Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
  
     If TextBox1.BorderStyle = BorderStyle.None Then
  
       TextBox1.BorderStyle = BorderStyle.Fixed3D
  
     Else
  
       TextBox1.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 it is not being hovered
over, change the background color back to white.

 Public Class Form1
  
   Dim WithEvents newText1 As New TextBox
  
   Dim WithEvents newText2 As New TextBox
  
   Dim WithEvents newText3 As New TextBox
  
   Dim WithEvents newLab1 As New Label
  
   Dim WithEvents newLab2 As New Label
  
   Dim WithEvents newLab3 As New Label
  
   Private Sub MouseEnter_newText1() Handles newText1.MouseEnter
  
     newText1.BackColor = Color.Blue
  
   End Sub
  
   Private Sub MouseEnter_newText2() Handles newText2.MouseEnter
  
       newText2.BackColor = Color.Red
  
   End Sub
  
   Private Sub MouseEnter_newText3() Handles newText3.MouseEnter
  
       newText3.BackColor = Color.Green
  
   End Sub
  
   Private Sub MouseEnter_neText1() Handles newText1.MouseLeave
  
     newText1.BackColor = Color.White
  
   End Sub
  
   Private Sub MouseEnter_neText2() Handles newText2.MouseLeave
  
     newText2.BackColor = Color.White
  
   End Sub
  
   Private Sub MouseEnter_neText3() Handles newText3.MouseLeave
  
     newText3.BackColor = Color.White
  
   End Sub
  
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     newText1.Top = 20
  
     newText1.Left = 100
  
     newText2.Top = 70
  
     newText2.Left = 100
  
     newText3.Top = 120
  
     newText3.Left = 100
  
     newLab1.Top = 20
  
     newLab1.Left = 10
  
     newLab2.Top = 70
  
     newLab2.Left = 10
  
     newLab3.Top = 120
  
     newLab3.Left = 10
  
     newLab1.Text = "Blue"
  
     newLab2.Text = "Red"
  
     newLab3.Text = "Green"
  
     Me.Controls.Add(newText1)
  
     Me.Controls.Add(newText2)
  
     Me.Controls.Add(newText3)
  
     Me.Controls.Add(newLab1)
  
     Me.Controls.Add(newLab2)
  
     Me.Controls.Add(newLab3)
  
   End Sub
  
 End Class  

4.) Create a form with 3 buttons
and a listbox.
Load 3 items into the listbox on load so that its not empty
Button 1: Select Item 1
Button 2: Select Item 2
Button 3: Select Item 3

 Public Class Form1
  
   Dim item1 As String = "one"
  
   Dim item2 As String = "two"
  
   Dim item3 As String = "three"
  
   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
     ListBox1.Items.Add(item1)
  
     ListBox1.Items.Add(item2)
  
     ListBox1.Items.Add(item3)
  
   End Sub
  
   Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  
     ListBox1.SelectedItem = item1
  
   End Sub
  
   Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  
     ListBox1.SelectedItem = item2
  
   End Sub
  
   Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
  
     ListBox1.SelectedItem = item3
  
   End Sub
  
 End Class  

Friday, February 11, 2011

Add a button to a page programmatically

Add a button to a page programmatically

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create new button
  
     Dim btnNew As New Button
  
     'set properties
  
     btnNew.Text = "CLICK ME"
  
     btnNew.Top = 45
  
     btnNew.Left = 175
  
     'Add the button to the form
  
     Me.Controls.Add(btnNew)
  
   End Sub
  
 End Class  

Add a textbox to a page programmatically

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create textbox
  
     Dim txtNew As TextBox = Nothing
  
     'set textbox properties
  
     txtNew = New Windows.Forms.TextBox
  
     txtNew.Name = "NewText"
  
     txtNew.Location = New System.Drawing.Point(6, 10)
  
     txtNew.Size = New System.Drawing.Size(250, 50)
  
     txtNew.TabIndex = 0
  
     'add text box to form
  
     Me.Controls.Add(txtNew)
  
   End Sub
  
 End Class  

Add a label to a page programmatically

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create textbox
  
     Dim lblNew As New Label
  
     'set textbox properties
  
     lblNew.SetBounds(10, 50, 100, 25)
  
     lblNew.Text = "I... Am a Label"
  
     'add text box to form
  
     Me.Controls.Add(lblNew)
  
   End Sub
  
 End Class  

Add an item list to a page programmatically
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'create item
  
     Dim lstAddress As ListBox
  
     lstAddress = New System.Windows.Forms.ListBox()
  
     'set list box properties
  
     lstAddress.IntegralHeight = False
  
     lstAddress.Location = New Point(20, 85)
  
     lstAddress.Name = "Adresses"
  
     lstAddress.Size = New System.Drawing.Size(250, 250)
  
     lstAddress.TabIndex = 0
  
     'add list box to form
  
     Me.Controls.Add(lstAddress)
  
   End Sub
  
 End Class  

Wednesday, February 9, 2011

Practice #2- Conditionals

1.)Mr. Spacely has been rethinking the company's shipping schemes. As an employee for Spacely's Sprockets, Spacely has asked you to write a quick program that will be used to determine shipping costs.

-A total purchase of items totaling under $50, we charge $5 shipping.
-A total purchase of items totaling $50 and over, there is no shipping charge.

 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim dblPurchase, dblShipping, dblTotal As Double
  
     'Input
  
     dblPurchase = CDbl(txtPurchase.Text)
  
     dblShipping = 6
  
     'Calculate
  
     If dblPurchase >= 50 Then
  
       dblTotal = dblPurchase
  
     Else
  
       dblTotal = (dblPurchase + dblShipping)
  
     End If
  
     'Display Total
  
     MessageBox.Show = ("Your total is " & FormatCurrency(dblTotal) & " .")
  
   End Sub
  
 End Class  

2.)You are looking for something to control your heat in your apartment and you discover there is NOT an app for that. It's about time that someone created one. You decide that you are the one to do it. Here's what you want to do.

-You want to turn the heat on when the temp has dropped below 72
-You also want to turn the AC on when the temp gets above 76

Your app should display in a message box if the heat is on, the AC is on, or if the system is idle. Plug in different temps for the room in a variable to see what the thermostat will do with it.
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim decTemp As Decimal
  
     'Get Input
  
     decTemp = CDec(txtTemperature.Text)
  
     'Calculate
  
     If decTemp < 72 Then
  
       MessageBox.Show("The heat is on.")
  
     ElseIf decTemp >= 72 And decTemp <= 76 Then
  
       MessageBox.Show("The system is idle")
  
     ElseIf decTemp > 76 Then
  
       MessageBox.Show("The A/C is on")
  
     End If
  
   End Sub
  
 End Class  

3.)You are working on a clothing website where people can buy kids socks. It's really hard for the customers to know what size they should buy for what age. It would be a good idea for the customer to have a tool to input their child's age and have the website suggest a size for them. Write a tool where you can set the age as a variable and have it suggest on of the sizes below:

a.0-2 yrs -XS
b.3-4 yrs -S
c.5-8 yrs -M
d.9-12 yrs -L
e.13+ yrs -XL
 Public Class Form1
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Declare variables    
  
     Dim intAge As Integer
  
     'Get Input
  
     intAge = CInt(txtAge.Text)
  
     'Calculate
  
     If intAge <= 2 Then
  
       MessageBox.Show("Your child's size is X-Small")
  
     ElseIf intAge >= 3 And intAge <= 4 Then
  
       MessageBox.Show("Your child's size is Small")
  
     ElseIf intAge >= 5 And intAge <= 8 Then
  
       MessageBox.Show("Your child's size is Medium")
  
     ElseIf intAge >= 9 And intAge <= 12 Then
  
       MessageBox.Show("Your child's size is Large")
  
     ElseIf intAge >= 13 Then
  
       MessageBox.Show("Your child's size is X-Large")
  
     End If
  
   End Sub
  
 End Class