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