Lecture 3 Code Samples Eric J. Schwabe IT 236 Spring 2008
Lecture 3 Code Samples Eric J. Schwabe IT 236 Spring 2008
Eric J. Schwabe
IT 236 Spring 2008
=====
' AddStrings.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates Add and Clear operations on a ListBox
' 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
=====
=====
' 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
' 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
Dim w As Double
Dim x As Double
Dim y As Double
Dim z As Double
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))
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
' 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
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
' 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
Dim w As Double
Dim x As Double
Dim y As Double
Dim z As Double
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))
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
feet = Double.Parse(feetTextBox.Text)
inches = feet * INCHES_PER_FOOT
inchesTextBox.Text = inches.ToString()
End Sub
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
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
' 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.
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.
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
' 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
End Sub
End Class
=====
=====
Option Strict On
' 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
End Sub
' When the user clicks the clear button, clears the contents of
' all text boxes
End Class
=====