Você está na página 1de 10

Lecture 6 Code Samples

Eric J. Schwabe
IT 236 Spring 2008

=====

' ListOfNames.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates various operations on a list box

Public Class Form1

' When the form first loads, displays the number of items in the list box
' in the text box below it (will be updated when it if changes)

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


Handles MyBase.Load
countTextBox.Text = outputListBox.Items.Count.ToString()
End Sub

' When the Add button is clicked, the string in the adjacent text box will be
' added to the end of the list of items in the list box

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


Handles addButton.Click
Dim newString As String = addStringTextBox.Text
outputListBox.Items.Add(newString)
countTextBox.Text = outputListBox.Items.Count.ToString()
End Sub

' When the Insert button is clicked, the string in the second text box will be
' inserted into the list of items in the list box at the position given by the
' value in the first text box -- if this value is not between 0 and Items.Count,
' an exception will generated and caught

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


System.EventArgs) Handles Button1.Click
Try
Dim newString As String = insertStringTextBox.Text
Dim position As Integer = Integer.Parse(insertPositionTextBox.Text)
outputListBox.Items.Insert(position, newString)
countTextBox.Text = outputListBox.Items.Count.ToString()
Catch
insertPositionTextBox.Text = "OOB!"
End Try
End Sub

' When the Lookup button is clicked, the list item in the position given by the value
' in the first text box will be displayed in the second text box; if the position is not
' between 0 and Items.Count-1, an exception will be generated and caught

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


System.EventArgs) Handles lookupButton.Click
Try
Dim position As Integer = Integer.Parse(lookupPositionTextBox.Text)
lookupStringTextBox.Text = outputListBox.Items(position)
Catch
lookupPositionTextBox.Text = "OOB!"
End Try
End Sub

' When the Set button is clicked, the list item in the position given by the value
' in the first text box will be changed to be the string given in the second text box --
' if position isn’t between 0 and Items.Count-1, exception will be generated and caught
Private Sub setButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles setButton.Click
Try
Dim position As Integer = Integer.Parse(setPositionTextBox.Text)
Dim newString As String = setStringTextBox.Text
outputListBox.Items(position) = newString
Catch
setPositionTextBox.Text = "OOB!"
End Try
End Sub

' When the RemoveAt button is clicked, the item in the position given by the value in the
' adjacent text box will be removed from the list of items in the list box; if position
' is not between 0 and Items.Count-1, an exception will be generated and caught

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


System.EventArgs) Handles removeAtButton.Click
Try
Dim position As Integer = Integer.Parse(removeAtPositionTextBox.Text)
outputListBox.Items.RemoveAt(position)
countTextBox.Text = outputListBox.Items.Count.ToString()
Catch
removeAtPositionTextBox.Text = "OOB!"
End Try
End Sub

' When the Remove button is clicked, the first occurrence of the string given in the
' text box will be removed from the list of items in the list box; if it does not appear
' there, nothing will happen (no exception will be generated)

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


System.EventArgs) Handles removeButton.Click
Dim oldString As String = removeStringTextBox.Text
outputListBox.Items.Remove(oldString)
countTextBox.Text = outputListBox.Items.Count.ToString()
End Sub
End Class

=====
=====

' IceCream.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates various combo boxes

Option Strict On

Public Class Form1

' When the button is clicked, if something has been selected from the list (or another
' string has been typed in), that string is copied to the bottom text box. If a new
' string was typed in (so that the selected index is -1), that new string ia added to
' the list of choices

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


Handles doneButton.Click

If (flavorsComboBox.Text <> "") Then ' if anything in the top text box...

flavorTextBox.Text = flavorsComboBox.Text ' copy the string to the text box


flavorsComboBox.Text = "" ' clear tne combo box

If flavorsComboBox.SelectedIndex = -1 Then ' if not a string from list,


flavorsComboBox.Items.Add(flavorTextBox.Text) ' add it to the list
End If

End If

End Sub
End Class

=====
=====

' Accmulate.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates a Do loop

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' Uses a Do loop to see how many years it will take for an initial
' investment to grow to a million dollars, given an annual rate of
' return.

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


Handles goButton.Click

Dim money As Double


Dim interest As Double
Dim year As Double = 0

money = Double.Parse(moneyTextBox.Text)
interest = Double.Parse(interestTextBox.Text)

outputListBox.Items.Clear()

' Here is the do loop...

Do While (money < 1000000)

outputListBox.Items.Add(year & ": " & money.ToString("C"))

year = year + 1
money = money * (1 + interest / 100)

Loop

' The do loop ends here...

outputListBox.Items.Add("You will be a millionaire")


outputListBox.Items.Add("in " & year & " years...")
outputListBox.Items.Add("...with " & money.ToString("C"))

End Sub
End Class

=====
=====

' Reprompt.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates a Do-Until loop

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' Uses a Do loop (Do-Until) to prompt the user for a positive integer,
' and re-prompt until the user enters a positive value.

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


MyBase.Load

Dim input As Integer

' Here is the do loop...

Do

Try
input = Integer.Parse(InputBox("Enter a positive integer:", _
"PROMPTING USER"))
MessageBox.Show("Your number is " & input, "ECHOING INPUT")
Catch
MessageBox.Show("Not a valid integer...", "INPUT ERROR")
End Try

Loop Until (input > 0)

' The do loop ends here...

numberTextBox.Text = input.ToString()

headingLabel.Select() 'This is so that text box isn't highlighted...

End Sub
End Class

=====
=====

' TryForLoops.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates various For loops

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' Counts through a range of values using a For loop...with or without commentary

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


goButton.Click

outputListBox.Items.Clear()

Dim count As Integer

'outputListBox.Items.Add("Starting loop... count is " & count.ToString())

For count = 1 To 10
'outputListBox.Items.Add
'("Test is True -- Executing loop body... count is " & count.ToString())
outputListBox.Items.Add("*** " & count & " ***")
Next

'outputListBox.Items.Add("Test is False -- Ending loop... count is " &


'count.ToString())

End Sub
End Class

=====
=====

' ReverseString.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Uses a For loop to traverse the characters of a string

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' When the button is clicked, the string in the upper text box
' is stored in a variable, and a new string is constructed that is
' the reverse of the input string. The reversed string is displayed
' in the lower text box.

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


System.EventArgs) Handles reverseButton.Click

Dim oldOne As String = inTextBox.Text


Dim newOne As String = ""
Dim current As String = ""
Dim i, len As Integer

len = oldOne.Length

' This loop steps through the characters in oldOne from


' back to front, reading the characters from position len - 1
' down to position 0

For i = len - 1 To 0 Step -1


current = oldOne.Substring(i, 1)
newOne = newOne & current

' outTextBox.Text = newOne


' MessageBox.Show("i is " & i & ", current is " & current, "Checking...")
Next

outTextBox.Text = newOne

End Sub
End Class

=====
=====

' ShowContents.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates using a loop to traverse the contents of a list box

Public Class Form1

' When the button is clicked, uses a loop to retrieve the items in the list box and
' display them, one at at time, in message boxes

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


Handles showButton.Click

Dim i As Integer
Dim nextDay As String

' Using a Do loop

i = 0
Do
nextDay = daysListBox.Items(i)
MessageBox.Show(nextDay, "Day " & i.ToString())
i = i + 1
Loop While i <= daysListBox.Items.Count - 1

' Using a For loop

'For i = 0 To daysListBox.Items.Count - 1
' nextDay = daysListBox.Items(i)
' MessageBox.Show(nextDay, "Day " & i.ToString())
'Next i

End Sub
End Class

=====
=====

' CeilingFunction.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates a user-defined function to round numbers up

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' When the button is clicked, the value in the upper text box
' is stored in a variable, and the Ceiling function is called
' to round it up to the next integer. The result is displayed
' in the lower text box.

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


Handles roundButton.Click

Dim oldNumber As Double = Double.Parse(inTextBox.Text)


Dim newNumber As Integer

newNumber = Ceiling(oldNumber)

outTextBox.Text = newNumber.ToString()

End Sub

' This user-defined function rounds the input value up to the


' nearest integer
' (Note that the Int function returns a Double, not an Integer,
' so we convert it with CInt...)

Function Ceiling(ByVal x As Double) As Integer

Dim result As Integer


result = CInt(-Int(-x))
Return result

End Function

End Class

=====
=====

' PizzaPrice.sln
' Eric J. Schwabe
' IT 236 Spring 2008
' Demonstrates a user-defined function

Option Strict On

Public Class Form1


Inherits System.Windows.Forms.Form

' When the button is clicked, read the pizza diameter and price
' from the two text boxes, and call the PricePerSquareInch function
' to compute the unit cost per square inch

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


System.EventArgs) Handles computeButton.Click

Dim diameter As Integer = Integer.Parse(diameterTextBox.Text)


Dim price As Double = Double.Parse(priceTextBox.Text)
Dim unitCost As Double

unitCost = PricePerSquareInch(diameter, price)

Dim output As String = diameter & "'' costs " & _


price.ToString("C") & "... " & _
unitCost.ToString("N2") & _
" cents per square inch"

outputListBox.Items.Add(output)

End Sub

' Given a diameter d and price p (given in dollars), computes the price
' per square inch (returned in cents)

Function PricePerSquareInch(ByVal d As Integer, ByVal p As Double) As Double

Dim area, cost As Double


area = 3.14159 * (d / 2) ^ 2
cost = (p * 100) / area
Return cost

End Function

End Class

=====

Você também pode gostar