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

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

The document contains code samples from several Visual Basic .NET projects created by Eric J. Schwabe for an IT class. The code samples demonstrate various concepts like adding and clearing items from a list box, displaying variable values, counting button clicks with a form variable, string conversions and methods, and getting user input with a message box. The final code sample shows a monthly budget example application.

Uploaded by

crutili
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

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

The document contains code samples from several Visual Basic .NET projects created by Eric J. Schwabe for an IT class. The code samples demonstrate various concepts like adding and clearing items from a list box, displaying variable values, counting button clicks with a form variable, string conversions and methods, and getting user input with a message box. The final code sample shows a monthly budget example application.

Uploaded by

crutili
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Lecture 3 Code Samples

Eric J. Schwabe
IT 236 Spring 2008

=====

' AddStrings.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates Add and Clear operations on a ListBox

Public Class Form1


Inherits System.Windows.Forms.Form

' When the button is clicked, add a string to the list box
Private Sub repeatButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles repeatButton.Click
cheerListBox.Items.Add("Hip Hip Hooray!")
End Sub

' When the button is clicked, clear the list box


Private Sub eraseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles eraseButton.Click
cheerListBox.Items.Clear()
End Sub
End Class

=====
=====

' NumberSpew.sln
' Eric J. Schawabe
' IT 236 Spring 2008
' Demonstrates the display of several variable values

Option Strict On
' Option Explicit is On by default

Public Class Form1


Inherits System.Windows.Forms.Form

' Declares and sets the values of four variables, and displays them in a list box
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles goButton.Click

' Variable declarations

Dim w As Double
Dim x As Double
Dim y As Double
Dim z As Double

' Assignments of values to variables

w = 2
x = -1
y = 9.876
z = 1 / 3

' w = Math.Sqrt(2)
' x = Math.Sqrt(14.5)
' y = Math.Round(Math.Sqrt(14.5), 3)
' z = Int(Math.Sqrt(14.5))

' Displaying values in the list box

outputListBox.Items.Clear()
outputListBox.Items.Add("w = " & w)
outputListBox.Items.Add("x = " & x)
outputListBox.Items.Add("y = " & y)
outputListBox.Items.Add("z = " & z)

End Sub
End Class

=====
=====

' CountClicks.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Counts the number of times the button is clicked, using a form-level variable

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' Declaration of a form-level variable to keep track of the number of clicks


Private count As Integer = 0

' Increments the value of the click counter when the button is pressed
Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnClick.Click

' This is a declaration of a local variable -- why don't we do this?


' Dim count As Integer = 0

count = count + 1
lblCount.Text = count.ToString()

End Sub
End Class

=====
=====

' NumberSpew.sln
' Eric J. Schawabe
' IT 236 Spring 2008
' Demonstrates the display of several variable values

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' Declares and sets the values of four variables, and displays them in a list box
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles goButton.Click

' Variable declarations

Dim interest As Double


Dim principal As Double

Dim w As Double
Dim x As Double
Dim y As Double
Dim z As Double

' Assignments of values to variables

Try
interest = Double.Parse(interestTextBox.Text)
principal = Double.Parse(principalTextBox.Text)

w = principal
x = principal * (1 + interest / 100)
y = principal * (1 + interest / 100) ^ 2
z = principal * (1 + interest / 100) ^ 3

' w = Math.Sqrt(2)
' x = Math.Sqrt(14.5)
' y = Math.Round(Math.Sqrt(14.5), 3)
' z = Int(Math.Sqrt(14.5))

' Displaying values in the list box

outputListBox.Items.Clear()
outputListBox.Items.Add("w = " & w)
outputListBox.Items.Add("x = " & x)
outputListBox.Items.Add("y = " & y)
outputListBox.Items.Add("z = " & z)

Catch ex As Exception
outputListBox.Items.Clear()
outputListBox.Items.Add("ERROR:")
outputListBox.Items.Add(" Enter number!")

End Try

End Sub
End Class

=====
=====

' Conversion.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Does conversions of feet to inches and vice versa

Option Strict On

Public Class Form1

' Variable to hold the conversion factor


Private Const INCHES_PER_FOOT As Double = 12

' Converts feet to inches by multiplying by 12


Private Sub feetInchesButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles feetInchesButton.Click

Dim feet As Double


Dim inches As Double

feet = Double.Parse(feetTextBox.Text)
inches = feet * INCHES_PER_FOOT
inchesTextBox.Text = inches.ToString()

End Sub

' Converts inches to feet by multiplying to 12


Private Sub inchesFeetButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles inchesFeetButton.Click

Dim feet As Double


Dim inches As Double

inches = Double.Parse(inchesTextBox.Text)
feet = inches / INCHES_PER_FOOT
feetTextBox.Text = feet.ToString()

End Sub

' Clears the inches text box when the user types in the feet text box
Private Sub feetTextBox_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles feetTextBox.KeyPress
inchesTextBox.Text = ""
End Sub

' Clears the feet text box when the user types in the inches text box
Private Sub inchesTextBox_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles inchesTextBox.KeyPress
feetTextBox.Text = ""
End Sub
End Class

=====
=====

' StringProperties.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrate some String object properties

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' Illustrates several properties of an input string


Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles goButton.Click

Dim incoming As String = inputTextBox.Text

outputListBox.Items.Clear()
outputListBox.Items.Add("String ='" & incoming & "'")
outputListBox.Items.Add("Length = " & incoming.Length)
outputListBox.Items.Add("ToUpper = '" & incoming.ToUpper & "'")
outputListBox.Items.Add("ToLower = '" & incoming.ToLower & "'")
outputListBox.Items.Add("Trim = '" & incoming.Trim & "'")
outputListBox.Items.Add("Trim Length = " & (incoming.Trim).Length)

End Sub
End Class

=====
=====

' StringMethods.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates some String object methods

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' Applies the Substring method to the string in the text box
' with the values of m and n taken from the left text boxes.
' The result is displayed in the lower left, and all text boxes
' on the right are cleared.

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


System.EventArgs) Handles substringButton.Click

Dim input As String


Dim m, n As Integer
Dim result As String

input = inputTextBox.Text
m = CInt(positionTextBox.Text)
n = CInt(sizeTextBox.Text)
result = input.Substring(m, n)

substringOutTextBox.Text = result
targetTextBox.Text = ""
indexOutTextBox.Text = ""

End Sub

' Applies the IndexOF method to the string in the text box
' with the value of target taken from the right text box.
' The result is displayed in the lower right, and all text boxes
' on the left are cleared.

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


System.EventArgs) Handles indexOfButton.Click

Dim input As String


Dim target As String
Dim result As Integer

input = inputTextBox.Text
target = targetTextBox.Text
result = input.IndexOf(target)

indexOutTextBox.Text = result.ToString()
positionTextBox.Text = ""
sizeTextBox.Text = ""
substringOutTextBox.Text = ""

End Sub
End Class

=====
=====

' HappyBirthday.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates the use of an input box, as well as a Form_Load event procedure

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' The procedures runs once, when the form is first displayed
' It prompts the user for a name using a text box, and sings Happy Birthday to that
person

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


Handles MyBase.Load

Dim name As String

name = InputBox("Please enter your name:", "Whose birthday is it?")

'MessageBox.Show("Happy Birthday, " & name & "!", "Additional Greeting")

songListBox.Items.Add("Happy Birthday to you,")


songListBox.Items.Add("Happy Birthday to you,")
songListBox.Items.Add("Happy Birthday, dear " & name & ",")
songListBox.Items.Add("Happy Birthday to you!")

End Sub
End Class

=====
=====

' Eric J. Schwabe


' IT 236 Section 501
' Monthly Budget Example
' January 18, 2006

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' When the user clicks the compute button, adds up the amounts
' to get the overall total, and computes the percentage of the
' budget taken up by each item. Displays the total and percentages
' in the read-only text boxes

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


Handles btnCompute.Click

' Find totals


Dim rent, food, util, ent, total As Double
rent = Double.Parse(txtRent.Text)
food = Double.Parse(txtFood.Text)
util = Double.Parse(txtUtil.Text)
ent = Double.Parse(txtEnt.Text)
total = rent + food + util + ent

' Find percentages


Dim rPct, fPct, uPct, ePct As Double
rPct = rent / total
fPct = food / total
uPct = util / total
ePct = ent / total

' Display results


txtTotal.Text = total.ToString("C")
TxtRentPct.Text = rPct.ToString("P")
txtFoodPct.Text = fPct.ToString("P")
txtUtilPct.Text = uPct.ToString("P")
txtEntPct.Text = ePct.ToString("P")
txtRent.Text = rent.ToString()
txtFood.Text = food.ToString()
txtUtil.Text = util.ToString()
txtEnt.Text = ent.ToString()

End Sub

' When the user clicks the clear button, clears the contents of
' all text boxes

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


Handles btnClear.Click
txtRent.Text = ""
txtFood.Text = ""
txtUtil.Text = ""
txtEnt.Text = ""
txtTotal.Text = ""
TxtRentPct.Text = ""
txtFoodPct.Text = ""
txtUtilPct.Text = ""
txtEntPct.Text = ""
End Sub

End Class

=====

You might also like