0% found this document useful (0 votes)
62 views7 pages

Chapter 4 Exercises

The document contains 10 exercises that demonstrate using Visual Basic .NET to create simple GUI applications with buttons, text boxes, labels, and other controls. The exercises show how to perform tasks like calculating grades, guessing games, payroll calculations, unit conversions, checkboxes to calculate prices, and more. Each exercise contains the necessary code to build the user interface and add event handler logic to respond to user input and display output.

Uploaded by

api-307845299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views7 pages

Chapter 4 Exercises

The document contains 10 exercises that demonstrate using Visual Basic .NET to create simple GUI applications with buttons, text boxes, labels, and other controls. The exercises show how to perform tasks like calculating grades, guessing games, payroll calculations, unit conversions, checkboxes to calculate prices, and more. Each exercise contains the necessary code to build the user interface and add event handler logic to respond to user input and display output.

Uploaded by

api-307845299
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CHAPTER 4 EXERCISES

Exercise 1
Public Class Form1
Dim Number As String
Dim Grade As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Grade = TextBox1.Text
If Grade > 70 Then
Me.Label2.Text = "Good Job!"
ElseIf Grade < 69 Then
Me.Label2.Text = "Study More!"
ElseIf Grade < 89 Then
Me.Label2.Text = "Great!"
End If
End Sub
End Class

Exercise 2
Public Class Form1
Const MIN As Integer = 1
Const Max As Integer = 50
Private Sub BnCheckGuess_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BnCheckGuess.Click
Randomize()
Static SecretNumber As Integer = Int((Max - MIN + 1) * Rnd() + MIN)
Dim Guess As Integer
Guess = Val(Me.TxtPlayerGuess.Text)
If Guess = SecretNumber Then
'correct
Me.LblMessage.Text = "You Guessed It!"
ElseIf Guess < SecretNumber Then
'Low Num
Me.LblMessage.Text = "Too Low."
ElseIf Guess > SecretNumber Then
'High Num
Me.LblMessage.Text = "Too High"
End If
End Sub
Private Sub TxtPlayerGuess_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TxtPlayerGuess.TextChanged
Me.LblMessage.Text = ""
End Sub
End Class

Exercise 3
Public Class Form1
Dim Hours As Decimal
Dim Rate As Decimal
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Hours = TextBoxNum1.Text
Rate = TextBoxNum2.Text
Label4.Text = Hours * Rate
If RadioButton1.Checked Then
Label4.Text = Hours * Rate
Label5.Text = "EXEMPT FROM TAXES"
ElseIf RadioButton2.Checked Then
Label4.Text = (Hours * Rate) - ((Hours * Rate) * 0.18)
End If
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton1.CheckedChanged
End Sub
End Class

Exercise 4
Public Class Form1
Dim Copy As Double
Dim Total As Double
Dim Amount As Double
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Label4.Text = Copy
Label5.Text = Total
Amount = TextBox1.Text
If Amount < 500 Then
Copy = "0.30"
Total = 0.3 * Amount
ElseIf Amount > 499 And TextBox1.Text < 749 Then
Copy = "0.28"
Total = 0.28 * Amount
ElseIf Amount > 750 And TextBox1.Text < 999 Then
Copy = "0.27"
Total = 0.27 * Amount
ElseIf Amount > 1000 Then
Copy = "0.25"
Total = 0.25 * Amount
End If
End Sub
End Class

Exercise 5
Public Class Form1
Dim Kilos As Single
Dim Centis As Single
Dim Centis2 As Single
Dim Centis3 As Single
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
TextBox1.Text = Kilos
TextBox2.Text = Centis
TextBox3.Text = Centis2
TextBox4.Text = Centis3
End Sub
End Class

Exercise 6
Public Class Form1
Dim Wait As Double
Dim Forward As Double
Dim ID As Double
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Label2.Text = 25.0 + Wait + Forward + ID
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CheckBox1.CheckedChanged
CheckBox1.Checked = Wait
Wait = 3.5
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CheckBox2.CheckedChanged
CheckBox2.Checked = Forward
Forward = 3.5
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CheckBox3.CheckedChanged
CheckBox3.Checked = ID
ID = 3.5
End Sub
End Class

Exercise 7
Public Class Form1
Dim Yes As String = "Y"
Dim No As String = "N"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If TextBox1.Text = "Y" And TextBox2.Text = "Y" Then
Label3.Text = "Contact Tech Support"
ElseIf TextBox1.Text = "Y" And TextBox2.Text = "N" Then
Label3.Text = "Check Drive Contacts"
ElseIf TextBox1.Text = "N" And TextBox2.Text = "Y" Then
Label3.Text = "Check The Speaker Connections"
ElseIf TextBox1.Text = "N" And TextBox2.Text = "N" Then
Label3.Text = "Bring the computer to a repair center"
End If
End Sub
End Class

Exercise 8

Public Class Form1


Dim Denied As Double
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Button2.Click
Application.Exit()
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Button1.Click
If TextBox1.Text = "Username" And TextBox2.Text = "Password" Then
MessageBox.Show("The ID and Password are correct.", "",
MessageBoxButtons.OK)
Application.Exit()
ElseIf TextBox1.Text = "Username" And TextBox2.Text <> "Password" Then
MessageBox.Show("The ID is correct but the password is incorrect.", "",
MessageBoxButtons.OK)
Denied = Denied + 1
TextBox1.Text = ""
ElseIf TextBox1.Text <> "Username" And TextBox2.Text = "Password" Then
MessageBox.Show("The ID is incorrect but the password is correct.", "",
MessageBoxButtons.OK)
Denied = Denied + 1
TextBox2.Text = ""
ElseIf TextBox1.Text <> "Username" And TextBox2.Text <> "Password" Then
MessageBox.Show("The ID and the password are incorrect.", "",
MessageBoxButtons.OK)
Denied = Denied + 1
TextBox1.Text = ""
TextBox2.Text = ""
End If
If Denied = 3 Then
MessageBox.Show("Sorry... Access denied.", "",
MessageBoxButtons.OK)
Application.Exit()
End If
End Sub
End Class

Exercise 9
Public Class Form1
Dim Num1 As Integer

Dim Num2 As Integer


Dim Answer As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Num1 = Rnd(100)
Num2 = Rnd(100)
Label1.Text = Num1
Label3.Text = Num2
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
Dim Answer As Double
Answer = Num1 + Num2
If TextBox1.Text = Answer Then
Label4.Text = "You Are Correct!"
Else
Label4.Text = "You Are Incorrect!"
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Answer = Num1 + Num2
TextBox1.Text = Answer
End Sub
End Class

Exercise 10
Public Class Form1
Dim Binder As Single
Dim Paper As Single
Dim Pens As Single

Dim Price As Single


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Label2.Text = Price
Label2.Text = Binder + Paper + Pens
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CheckBox1.CheckedChanged
CheckBox1.Checked = Binder
Binder = 5.99
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CheckBox2.CheckedChanged
CheckBox2.Checked = Paper
Paper = 3.99
If CheckBox2.Checked Then
Label3.Text = "The paper size is 8 1/2 inches by 11 inches"
End If
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CheckBox3.CheckedChanged
CheckBox3.Checked = Pens
Pens = 8.99
End Sub
End Class

You might also like