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  

Friday, February 11, 2011

Practice #2 Spacely, Heat & Clothing

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.
     Dim purchase, shipping, total As Double
  
     shipping = 5
  
     purchase = CDbl(txtPurchase.Text)
  
     If purchase < 50 Then
  
       total = purchase + shipping
  
     ElseIf purchase >= 50 Then
  
       total = purchase
  
     End If
  
     MessageBox.Show("The total of the item plus shipping is " & FormatCurrency(total))
  
   End Sub  
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.
 Dim temperature As Double
  
 temperature = CDbl(txtTemp.Text)
  
 If temperature <= 72 Then
  
 MessageBox.Show("The heat is on.", "Heat")
  
 ElseIf temperature > 72 And temperature < 76 Then
  
 MessageBox.Show("The system is idle", "Idle System")
  
 ElseIf temperature >= 76 Then
  
 MessageBox.Show("The A/C is on.", "A/C")
  
 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 years - XS
b. 3-4 years - S
c. 5-8 years - M
d. 9-12 years - L
e. 13+ years - XL
     Dim age As Double
  
     age = CDbl(txtpurchase.Text)
  
     'Calculate total 
  
     If age <= 2 Then
  
       MessageBox.Show("The child's size is XS.", "XS")
  
     ElseIf age >= 3 And age <= 4 Then
  
       MessageBox.Show("The child's size is Small", "Small")
  
     ElseIf age >= 5 And age <= 8 Then
  
       MessageBox.Show("The child's size is Medium", "Medium")
  
     ElseIf age >= 9 And age <= 12 Then
  
       MessageBox.Show("The child's size is Large", "Large")
  
     ElseIf age >= 13 Then
  
       MessageBox.Show("The child's size is X-Large.", "X-Large")
  
     End If
  
   End Sub  

Case Statements

The following prgram converts the finishing position in a horse race into a descriptive phrase.  After the variable position is assigned a value from txtPosition, VB searches for the first Case clause whose value list contains that value and executes the succeeding statement.  If the value of position is greater than 5, then the statement following Case Else is executed.
 Dim position As Integer   'selector
  
 position = CInt(txtPosition.Text)
  
 Select Case position
  
 Case 1
  
 txtOutcome.Text = "Win"
  
 Case 2
  
 txtOutcome.Text = "Place"
  
 Case 3
  
 txtOutcome.Text = "Show"
  
 Case 4, 5 
  
 txtOutcome.Text = "You almost placed in the money."
  
 Case Else
  
 txtOutcome.Text = "Out of the money."
  
 End Select
  
 End Sub
  

If Statements

A conditional expression is formed by relating values, either constants or variables . The comparison is evaluated as Boolean True or False, and the script reacts to that condition. The following script presents a simple example of script decision making, making one decision.
  If Args.CommandName = "Button 1" Then
  
   ButtonResponse.Text = "You clicked Button 1"
  
  End If
  
 End Sub
  
This shows an If statement of either or:
     Dim Message As String
  
     Message = "Skittles is an old form of bowling in which a wooden " & "disk is used to knowck down nine pins arranged in a square."
  
     If txtmessage.Text.ToUpper = "N" Then
  
       MessageBox.Show(Message, "Definition")
  
     End If
  
     txtQuote.Text = "Life ain't all beer and skittles." & " - Du Maurier (1894)"
  
   End Sub
  
 End Class  

This can make many decision depending on what it asks for, here it's honor students according to a certain GPA.
    Dim gpa As Double = CDbl(txtGpa.Text) 
  
    Dim honors As String 
  
    If gpa >= 3.9 Then 
  
     honors = " summa cum laude." 
  
    ElseIf gpa >= 3.6 Then 
  
     honors = " magna cum laude." 
  
    ElseIf gpa >= 3.3 Then 
  
     honors = " cum laude." 
  
    ElseIf gpa >= 2 Then 
  
     honors = "." 
  
    End If 
  
    txtResults.Text = "You graduated" & honors 
  
   End Sub 
  

Wednesday, February 9, 2011

Adding code to a page programmatically

To add a button to a page just by using code, we use Me.Controlls.Add(button).  The following is adding a button that says "go". 

     Dim btn As New Button
  
     btn.Text = "go"
  
     btn.Top = 45
  
     btn.Left = 190
  
     Me.Controls.Add(btn)  

This is to add a textbox to a form using code. Using dynamic text and also using Me.Controls.Add(textbox).
     Dim dynamicText As TextBox = Nothing
  
     dynamicText = New Windows.Forms.TextBox
  
     dynamicText.Name = "TimeTextBox"
  
     dynamicText.Location = New System.Drawing.Point(8, 8)
  
     dynamicText.Size = New System.Drawing.Size(232, 20)
  
     dynamicText.TabIndex = 0
  
     Me.Controls.Add(dynamicText)
  
   End Sub  

Adding a label to a form using code is pretty short.  You can set the boundaries of where the label will sit using lbl.SetBounds.
     Dim lbl As New Label
  
     lbl.SetBounds(10, 50, 100, 25)
  
     lbl.Text = "Hello World!"
  
     Me.Controls.Add(lbl)
  
   End Sub  

Here is how to add a listbox using code, also including the size and point of where the listbox is to be placed.
     Dim listBox1 As New ListBox()
  
     listBox1.Size = New System.Drawing.Size(200, 100)
  
     listBox1.Location = New System.Drawing.Point(10, 10)
  
     Me.Controls.Add(listBox1)
  
   End Sub  

The DataGridView control can display rows of data from a data source. You can extend the DataGridView control in a number of ways to build custom behaviors into your applications. The ReadOnly property indicates whether the data displayed by the cell can be edited or not. You can set ReadOnly Property in three levels. You can make entire dataGridView as ReadOnly.
 dataGridView1.ReadOnly = true
  

You can make entire row as ReadOnly:
 dataGridView1.Rows(index).ReadOnly = true  

You can make entire Column as ReadOnly
 dataGridView1.Columns(index).ReadOnly = true