0% found this document useful (0 votes)
35 views

Lecture 5 Code Samples Eric J. Schwabe IT 236 Spring 2008

This document contains code samples from several Visual Basic .NET applications that demonstrate different programming concepts like branching, selecting cases, and updating labels and text based on user input. The code samples include applications to compute employee salaries based on type, check cholesterol levels and provide messages, convert numerical grades to letter grades, and process a burger order with customizations. Each sample application contains code and comments to illustrate the concept being demonstrated.

Uploaded by

crutili
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Lecture 5 Code Samples Eric J. Schwabe IT 236 Spring 2008

This document contains code samples from several Visual Basic .NET applications that demonstrate different programming concepts like branching, selecting cases, and updating labels and text based on user input. The code samples include applications to compute employee salaries based on type, check cholesterol levels and provide messages, convert numerical grades to letter grades, and process a burger order with customizations. Each sample application contains code and comments to illustrate the concept being demonstrated.

Uploaded by

crutili
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture 5 Code Samples

Eric J. Schwabe
IT 236 Spring 2008

=====

' Eric J. Schwabe


' Lab2Demo.sln
' IT 236 Section 501
' Winter 2006
' Lab Exercise 2 Sample Solution

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

Private count As Integer = 1


Private total As Double = 0

' Reads the employee's name and type, and computes and displays their
' monthly salary appropriately when the type is S, H, or U. Displays
' an errors message for all other employee types.

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnCompute.Click

Dim name As String


Dim type As String
Dim salary As Double
Dim fmtStr As String = "{0, -16} ({1, 1}){2, 20:C2}"

name = txtName.Text
type = (txtType.Text).Trim

Select Case type


Case "S"
salary = CDbl(InputBox("Enter the monthly salary:", _
"Salaried Employee"))
Case "H"
Dim hours As Double = CDbl(InputBox("Enter the hours worked:", _
"Hourly Employee"))
Dim rate As Double = CDbl(InputBox("Enter the hourly rate:", _
"Hourly Employee:"))
salary = hours * rate
Case "U"
salary = 0
End Select

If (type = "S") Or (type = "H") Or (type = "U") Then


lstOutput.Items.Add(count.ToString() & ". " & name & " (" & type & "), " &
salary.ToString("C"))
txtName.Text = ""
txtType.Text = ""
count = count + 1
total = total + salary
Label1.Text = total.ToString("C")
Else
MessageBox.Show("Employee type must be S, H, or U -- please re-enter.", "ERROR
CONDITION")
txtType.Text = ""
txtType.Focus()
End If

End Sub
Private Sub lstOutput_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles lstOutput.SelectedIndexChanged

End Sub
End Class

=====
=====

' Cholesterol.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates branching with if statements

Public Class Form1

' Read the user's cholesterol level from the text box, and check to see if it is okay
' (at most 200), slightly elevated (200-220), or considerable elevated (above 220).
' Give a appropriate reply in the label

Private Sub checkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles checkButton.Click

' Constants for the cutoffs, and a variable to hold the input value

Const MODERATE_CUTOFF As Integer = 200


Const HIGH_CUTOFF As Integer = 220
Dim cholesterol As Double

' Read in the user's value, and branch to give the appropriate reply

Try

cholesterol = Double.Parse(levelTextBox.Text)

If cholesterol <= MODERATE_CUTOFF Then


reportLabel.Text = "You're fine..."
ElseIf cholesterol <= HIGH_CUTOFF Then
reportLabel.Text = "That's a little high... eat less fat and exercise
more..."
Else
reportLabel.Text = "That's quite high... you may want to consider
medications..."
End If

Catch ex As Exception

reportLabel.Text = "Please enter a number!"


levelTextBox.Clear()
levelTextBox.Focus()

End Try

End Sub

' Whenever the contents of the text box are modified, clear the label to insure that
' the text box cannot be changed to disagree witih the label

Private Sub levelTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)


Handles levelTextBox.TextChanged
reportLabel.Text = ""
End Sub
End Class

=====
=====

' LetterGrade.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates the Case statement

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' When the button is clicked, the students numerical grade is


' read from a text box and converted to a Double. It is
' the converted to letter grade of A, B, C, D, or F,
' which is displayed

Private Sub computeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)


Handles computeButton.Click

Dim score As Double = Double.Parse(scoreTextBox.Text)


Dim grade As String = "IN"

Debug.WriteLine(score)

' Using a Select Case to set the grade...

Select Case (score)


Case Is >= 90
grade = "A"
Case Is >= 80
grade = "B"
Case Is >= 70
grade = "C"
Case Is >= 60
grade = "D"
Case Else
grade = "F"
End Select

gradeTextBox.Text = grade

Debug.WriteLine(grade)

End Sub

' Clear both text boxes when the user goes back to the input text box

Private Sub scoreTextBox_Enter(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles scoreTextBox.Enter

gradeTextBox.Text = ""
scoreTextBox.Text = ""

End Sub

End Class

=====
=====

' Order.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Final branchign example

Option Strict On

Public Class Form1

' Whenever a radio button is checked, update the label


Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles singleRadioButton.CheckedChanged, _
doubleRadioButton.CheckedChanged,
tripleRadioButton.CheckedChanged
If singleRadioButton.Checked = True Then
burgerLabel.Text = "Single"
ElseIf doubleRadioButton.Checked = True Then
burgerLabel.Text = "Double"
ElseIf tripleRadioButton.Checked = True Then
burgerLabel.Text = "Triple"
End If
End Sub

' When the cheese box is toggled, update the label


Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cheeseCheckBox.CheckedChanged
If cheeseCheckBox.Checked Then
cheeseLabel.Text = "with Cheese"
Else
cheeseLabel.Text = ""
End If
End Sub

' When the mustard box is toggled, update the label


Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mustardCheckBox.CheckedChanged
If mustardCheckBox.Checked Then
mustardLabel.Text = "with Mustard"
Else
mustardLabel.Text = ""
End If
End Sub

' When the ketchup box is toggled, update the label


Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ketchupCheckBox.CheckedChanged
If ketchupCheckBox.Checked Then
ketchupLabel.Text = "with Ketchup"
Else
ketchupLabel.Text = ""
End If
End Sub

' When the button is clicked, see which radio button is checked, and which check boxes
' are checked, and display the specified order and total
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles processButton.Click

Dim output As String = ""


Dim total As Double

If singleRadioButton.Checked Then
orderListBox.Items.Add("Single Burger")
total = 1.99
ElseIf doubleRadioButton.Checked Then
orderListBox.Items.Add("Double Burger")
total = 2.99
ElseIf tripleRadioButton.Checked Then
orderListBox.Items.Add("Triple Burger")
total = 3.99
End If

If cheeseCheckBox.Checked Then
orderListBox.Items.Add(" with Cheese")
total = total + 0.5
End If

If ketchupCheckBox.Checked Then
orderListBox.Items.Add(" with Ketchup")
End If

If mustardCheckBox.Checked Then
orderListBox.Items.Add(" with Mustard")
End If

orderListBox.Items.Add("Total: " & total.ToString("C"))


orderListBox.Items.Add("")

End Sub
End Class

=====

You might also like