Lecture 3 Code Samples
Eric J. Schwabe
IT 236 Spring 2008
=====
' [Link]
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates Add and Clear operations on a ListBox
Public Class Form1
Inherits [Link]
' When the button is clicked, add a string to the list box
Private Sub repeatButton_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
[Link]("Hip Hip Hooray!")
End Sub
' When the button is clicked, clear the list box
Private Sub eraseButton_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link]()
End Sub
End Class
=====
=====
' [Link]
' 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 [Link]
' Declares and sets the values of four variables, and displays them in a list box
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
' 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 = [Link](2)
' x = [Link](14.5)
' y = [Link]([Link](14.5), 3)
' z = Int([Link](14.5))
' Displaying values in the list box
[Link]()
[Link]("w = " & w)
[Link]("x = " & x)
[Link]("y = " & y)
[Link]("z = " & z)
End Sub
End Class
=====
=====
' [Link]
' 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 [Link]
' 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 [Link], ByVal e As [Link])
Handles [Link]
' This is a declaration of a local variable -- why don't we do this?
' Dim count As Integer = 0
count = count + 1
[Link] = [Link]()
End Sub
End Class
=====
=====
' [Link]
' Eric J. Schawabe
' IT 236 Spring 2008
' Demonstrates the display of several variable values
Option Strict On
Public Class Form1
Inherits [Link]
' Declares and sets the values of four variables, and displays them in a list box
Private Sub Button1_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
' 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 = [Link]([Link])
principal = [Link]([Link])
w = principal
x = principal * (1 + interest / 100)
y = principal * (1 + interest / 100) ^ 2
z = principal * (1 + interest / 100) ^ 3
' w = [Link](2)
' x = [Link](14.5)
' y = [Link]([Link](14.5), 3)
' z = Int([Link](14.5))
' Displaying values in the list box
[Link]()
[Link]("w = " & w)
[Link]("x = " & x)
[Link]("y = " & y)
[Link]("z = " & z)
Catch ex As Exception
[Link]()
[Link]("ERROR:")
[Link](" Enter number!")
End Try
End Sub
End Class
=====
=====
' [Link]
' 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 [Link], ByVal e As
[Link]) Handles [Link]
Dim feet As Double
Dim inches As Double
feet = [Link]([Link])
inches = feet * INCHES_PER_FOOT
[Link] = [Link]()
End Sub
' Converts inches to feet by multiplying to 12
Private Sub inchesFeetButton_Click(ByVal sender As [Link], ByVal e As
[Link]) Handles [Link]
Dim feet As Double
Dim inches As Double
inches = [Link]([Link])
feet = inches / INCHES_PER_FOOT
[Link] = [Link]()
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
[Link]) Handles [Link]
[Link] = ""
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
[Link]) Handles [Link]
[Link] = ""
End Sub
End Class
=====
=====
' [Link]
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrate some String object properties
Option Strict On
Public Class Form1
Inherits [Link]
' Illustrates several properties of an input string
Private Sub btnGo_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
Dim incoming As String = [Link]
[Link]()
[Link]("String ='" & incoming & "'")
[Link]("Length = " & [Link])
[Link]("ToUpper = '" & [Link] & "'")
[Link]("ToLower = '" & [Link] & "'")
[Link]("Trim = '" & [Link] & "'")
[Link]("Trim Length = " & ([Link]).Length)
End Sub
End Class
=====
=====
' [Link]
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates some String object methods
Option Strict On
Public Class Form1
Inherits [Link]
' 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 [Link], ByVal e As
[Link]) Handles [Link]
Dim input As String
Dim m, n As Integer
Dim result As String
input = [Link]
m = CInt([Link])
n = CInt([Link])
result = [Link](m, n)
[Link] = result
[Link] = ""
[Link] = ""
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 [Link], ByVal e As
[Link]) Handles [Link]
Dim input As String
Dim target As String
Dim result As Integer
input = [Link]
target = [Link]
result = [Link](target)
[Link] = [Link]()
[Link] = ""
[Link] = ""
[Link] = ""
End Sub
End Class
=====
=====
' [Link]
' 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 [Link]
' 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 [Link], ByVal e As [Link])
Handles [Link]
Dim name As String
name = InputBox("Please enter your name:", "Whose birthday is it?")
'[Link]("Happy Birthday, " & name & "!", "Additional Greeting")
[Link]("Happy Birthday to you,")
[Link]("Happy Birthday to you,")
[Link]("Happy Birthday, dear " & name & ",")
[Link]("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 [Link]
' 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 [Link], ByVal e As [Link])
Handles [Link]
' Find totals
Dim rent, food, util, ent, total As Double
rent = [Link]([Link])
food = [Link]([Link])
util = [Link]([Link])
ent = [Link]([Link])
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
[Link] = [Link]("C")
[Link] = [Link]("P")
[Link] = [Link]("P")
[Link] = [Link]("P")
[Link] = [Link]("P")
[Link] = [Link]()
[Link] = [Link]()
[Link] = [Link]()
[Link] = [Link]()
End Sub
' When the user clicks the clear button, clears the contents of
' all text boxes
Private Sub btnClear_Click(ByVal sender As [Link], ByVal e As [Link])
Handles [Link]
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
End Sub
End Class
=====