0% found this document useful (0 votes)
66 views8 pages

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

This document contains code samples from several Visual Basic .NET projects created by Eric J. Schwabe for an IT class. The samples demonstrate various programming concepts like comparisons, logical operators, branching, input validation, and checking multiple checkboxes. Each sample is preceded by comments describing what the code demonstrates.

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)
66 views8 pages

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

This document contains code samples from several Visual Basic .NET projects created by Eric J. Schwabe for an IT class. The samples demonstrate various programming concepts like comparisons, logical operators, branching, input validation, and checking multiple checkboxes. Each sample is preceded by comments describing what the code demonstrates.

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/ 8

Lecture 4 Code Samples

Eric J. Schwabe
IT 236 Spring 2008

=====

' Comparisons.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Display the result of various numerical and string comparisons

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' Compares two strings (or numbers) in all possible ways,


' and displays the results...

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


System.EventArgs) Handles compareButton.Click

Dim X, Y As String
X = xTextBox.Text()
Y = yTextBox.Text()

'Later...
'Dim X, Y As Double
'X = Double.Parse(xTextBox.Text)
'Y = Double.Parse(yTextBox.Text)

displayListBox.Items.Clear()
displayListBox.Items.Add(X & " = " & Y & " : " & (X = Y))
displayListBox.Items.Add("")
displayListBox.Items.Add(X & " <> " & Y & " : " & (X <> Y))
displayListBox.Items.Add("")
displayListBox.Items.Add(X & " < " & Y & " : " & (X < Y))
displayListBox.Items.Add("")
displayListBox.Items.Add(X & " <= " & Y & " : " & (X <= Y))
displayListBox.Items.Add("")
displayListBox.Items.Add(X & " > " & Y & " : " & (X > Y))
displayListBox.Items.Add("")
displayListBox.Items.Add(X & " >= " & Y & " : " & (X >= Y))

End Sub
End Class

=====
=====

' CompoundComparisons.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrate compound conditions using logical operators

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' Checks if an input exam score is legal (in range from 0 to 100)
' and if it is either very low (<65) or very high (>90)

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


Handles checkButton.Click

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

displayListBox.Items.Clear()
displayListBox.Items.Add("Is " & score & " between 0 and 100?")
displayListBox.Items.Add((score >= 0) And (score <= 100))
displayListBox.Items.Add("")
displayListBox.Items.Add("Is " & score & " below 65 or above 90?")
displayListBox.Items.Add((score < 65) Or (score > 90))

' We'll do these later...

'Dim a As Integer = 2
'Dim b As Integer = 3

'displayListBox.Items.Add("")
'displayListBox.Items.Add(3 * a = 2 * b)
'displayListBox.Items.Add((5 - a) * b < 7)
'displayListBox.Items.Add((a < b) And (b < a))
'displayListBox.Items.Add((a < b) Or (b < a))
'displayListBox.Items.Add((a * a) < b Xor (a * a) < a)
'displayListBox.Items.Add("car" < "train")
'displayListBox.Items.Add("Inspector" <= "guard")
'displayListBox.Items.Add(Not ("B" = "b"))
'displayListBox.Items.Add("pick" > "pitcher")

End Sub
End Class

=====
=====

' SSTax.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Uses branching to compute social security tax for a given income

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' When the button is clicked, the user's income is read from a
' text box and converted to a Double. If the value is at most
' maxIncome, the tax is taxRate times the income. If the value is
' more than maxIncome, the tax is taxRate times maxIncome.

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


Handles computeButton.Click

Const maxIncome As Double = 102000 ' $102,000.00


Const taxRate As Double = 0.062 ' 6.2%
Dim income As Double
Dim ssTax As Double

income = Double.Parse(incomeTextBox.Text)

If (income <= maxIncome) Then


ssTax = taxRate * income
Else
ssTax = taxRate * maxIncome
End If

incomeTextBox.Text = income.ToString("C")
taxTextBox.Text = ssTax.ToString("C")

End Sub

End Class

=====
=====

' HiLoGame.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' The user tries to guess a randomly-generated number between 1 and 100

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' The form-level variable storing the value to be guessed

Dim target As Integer

' Sets up the initial, random value of target

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


MyBase.Load

' Create and a random number generator


Dim randomNum As Random = New Random(DateTime.Now.Millisecond)
' This line generates a random number from 1 to 100
target = randomNum.Next(1, 100)

End Sub

' Given a user's guess in the text box, first check that is in
' the range from 1 to 100. If it is, state whether the guess is
' too low, too high, or correct.

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


Handles guessButton.Click

Dim guess As Integer = Integer.Parse(guessTextBox.Text)


Dim reply As String

If ((guess < 1) Or (guess > 100)) Then


reply = "OUT OF BOUNDS!"
ElseIf (guess < target) Then
reply = "Too low..."
ElseIf (guess > target) Then
reply = "Too high..."
Else
reply = "RIGHT!"
End If

replyTextBox.Text = reply

End Sub

' Clears the reply box when the input is changed

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


System.EventArgs) Handles guessTextBox.TextChanged
replyTextBox.Text = ""
End Sub
End Class

=====
=====

' Eric J. Schwabe


' IT 236 Section 501
' Winter 2006

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 btnCompute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
btnCompute.Click
Dim score As Double = CDbl(txtScore.text)
Dim grade As String = "IN"

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

Select Case (score)


Case 90 To 100
grade = "A"
Case 80 To 89
grade = "B"
Case 70 To 79
grade = "C"
Case 60 To 69
grade = "D"
Case Else
grade = "F"
End Select

txtGrade.Text = grade

End Sub

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


System.EventArgs) Handles txtScore.TextChanged
txtGrade.Text = ""
End Sub
End Class

=====
=====

' Envelope.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Do input validation while constructing the mailing address for an envelope

Public Class Form1

' Read the contents of the text boxes, and address the envelope as long as all of the
' required fields have been filled in. Also check if the optional fields have been
filled
' in, and if they have not, omit them from the address.

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


Handles makeButton.Click

Dim firstName, middleName, lastName As String


Dim address, city, state, zip As String
Dim THIRTY_SPACES As String = " "

firstName = firstTextBox.Text
middleName = middleTextBox.Text
lastName = lastTextBox.Text
address = addressTextBox.Text
city = cityTextBox.Text
state = stateTextBox.Text
zip = zipMaskedTextBox.Text

' Check that all required fields are filled in


If (firstName.Length > 0) And (lastName.Length > 0) And (address.Length > 0) _
And (city.Length > 0) And (state.Length > 0) Then

' Check that entered values have the correct length

envelopeListBox.Items.Clear()
envelopeListBox.Items.Add("DePaul CDM")
envelopeListBox.Items.Add("243 S. Wabash Ave.")
envelopeListBox.Items.Add("Chicago, IL 60604")
envelopeListBox.Items.Add("")
envelopeListBox.Items.Add("")

If (middleName.Length > 0) Then


envelopeListBox.Items.Add(THIRTY_SPACES & firstName & " " & middleName & " "
& lastName)
Else
envelopeListBox.Items.Add(THIRTY_SPACES & firstName & " " & lastName)
End If

envelopeListBox.Items.Add(THIRTY_SPACES & address)

If (zip.Length > 0) Then


envelopeListBox.Items.Add(THIRTY_SPACES & city & ", " & state & " " & zip)
Else
envelopeListBox.Items.Add(THIRTY_SPACES & city & ", " & state)
End If

Else
MessageBox.Show("Some required field has not been filled in.", "Error")

End If
End Sub
End Class

=====
=====

' Headshot.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrate the independent checking of several check boxes with if statements

Public Class Form1

' When any of the first four check boxes are changed, check each one and display the
picture,
' name, address, and email depending on which are checked. If either the address or
email
' check boxes are checked, enable the highlighting check box

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


System.EventArgs) Handles pictureCheckBox.CheckedChanged, _
nameCheckBox.CheckedChanged, addressCheckBox.CheckedChanged,
emailCheckBox.CheckedChanged

' Display the picture?


If pictureCheckBox.Checked Then
facePictureBox.Visible = True
Else
facePictureBox.Visible = False
End If

' Display the name?


If nameCheckBox.Checked Then
nameLabel.Visible = True
Else
nameLabel.Visible = False
End If

' Display the address?


If addressCheckBox.Checked Then
addressListBox.Visible = True
Else
addressListBox.Visible = False
End If

' Display the email?


If emailCheckBox.Checked Then
emailLabel.Visible = True
Else
emailLabel.Visible = False
End If

' Display the highlighting check box?


' (if either address or email is displayed, or both)
If addressCheckBox.Checked Or emailCheckBox.Checked Then
newInfoCheckBox.Enabled = True
Else
newInfoCheckBox.Enabled = False
newInfoCheckBox.Checked = False
End If

End Sub

' If the highlighting check box is on, display the address and email in red,
' and if it is not, display them in black

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


System.EventArgs) Handles newInfoCheckBox.CheckedChanged
If newInfoCheckBox.Checked Then
addressListBox.ForeColor = Color.Red
emailLabel.ForeColor = Color.Red
Else
addressListBox.ForeColor = Color.Black
emailLabel.ForeColor = Color.Black
End If
End Sub
End Class

=====

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy