vb lab - MCST-CS

advertisement
Chapter 6
Arrays, Random, Functions,
Subroutines
Application #1
Global and Form loads
Public Class EnhancedCafeteriaSurvey
' two-dimensional array stores voting results
Dim votes(3, 1) As Integer
' handles Form's Load event
Private Sub EnhancedCafeteriaSurvey_Load(ByVal sender As
System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
foodsComboBox.SelectedIndex = 0 ' select first food in
list
End Sub ' EnhancedCafeteriaSurvey_Load
' handles Vote Button's Click event
Private Sub voteButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles voteButton.Click
Dim index As Integer = foodsComboBox.SelectedIndex
' count the vote
If likeRadioButton.Checked = True Then
votes(index, 0) += 1
Else
votes(index, 1) += 1
End If
DisplayVotes() ' display the voting results
End Sub ' voteButton_Click
' display the voting results in the ListBox
Sub DisplayVotes()
resultsListBox.Items.Clear() ' clear the previous results
' display the ListBox header
resultsListBox.Items.Add("Menu Item" & vbTab &
"Like" & vbTab & "Dislike")
' add voting results for each food option
For counter As Integer = 0 To votes.GetUpperBound(0)
resultsListBox.Items.Add(
foodsComboBox.Items(counter).ToString() &
vbTab & votes(counter, 0) &
vbTab & votes(counter, 1))
Next
End Sub ' DisplayVotes
Function call
' display the voting results in the ListBox
Sub DisplayVotes()
resultsListBox.Items.Clear() ' clear the previous results
' display the ListBox header
resultsListBox.Items.Add("Menu Item" & vbTab &
"Like" & vbTab & "Dislike")
' add voting results for each food option
For counter As Integer = 0 To votes.GetUpperBound(0)
resultsListBox.Items.Add(
foodsComboBox.Items(counter).ToString() &
vbTab & votes(counter, 0) &
vbTab & votes(counter, 1))
Next
End Sub ' DisplayVotes
Application #2
Public Class GuessTheNumber
Dim randomObject As New Random()
Dim number As Integer = randomObject.Next(1, 101)
' handles Enter button click event
Private Sub enterButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles enterButton.Click
' retrieve the user's guess
Dim guess As Integer = Convert.ToInt32(guessTextBox.Text)
' check answer
If guess = number Then
outputLabel.Text = "Correct!"
enterButton.Enabled = False
newGameButton.Enabled = True
ElseIf guess > number Then
outputLabel.Text = "Too high..."
Else
outputLabel.Text = "Too low..."
End If
guessTextBox.Focus() ' give focus to the TextBox
End Sub ' enterButton_Click
Private Sub newGameButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles newGameButton.Click
' start a new game
number = randomObject.Next(1, 101) ' generate new number
enterButton.Enabled = True ' enable the Enter Button
newGameButton.Enabled = False ' disable the New Game Button
outputLabel.Text = "" ' clear result
guessTextBox.Clear() ' clear the previous guess
guessTextBox.Focus()
End Sub ' newGameButton_Click
' handles Guess TextBox's TextChanged event
Private Sub guessTextBox_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles guessTextBox.TextChanged
outputLabel.Text = String.Empty ' clear result
End Sub ' guessTextBox_TextChanged
Public Class RoadSignTest
' String array stores sign names
Dim options() As String = {
"Do Not Enter", "Narrow bridge", "No bicycles",
"No left turn", "No Pedestrians", "No U-turn",
"Road Narrows", "Stop", "Stop sign ahead",
"Traffic signals ahead", "Winding road ahead", "Yield"}
' Boolean array tracks displayed signs
Dim used(options.GetUpperBound(0)) As Boolean
Dim count As Integer = 1 ' number of signs shown
Dim correctAnswer As Integer ' index of current sign
' handles Road Sign Test Form's Load event
Private Sub RoadSignTest_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Array.Sort(options) ' alphabetize sign names
' display sign names in ComboBox
optionsComboBox.DataSource = options
DisplaySign() ' display first sign in PictureBox
End Sub ' RoadSignTest_Load
' display random sign in PictureBox
Sub DisplaySign()
' unique index ensures that a sign is used no more than once
correctAnswer = GetUniqueRandomNumber()
' retrieve specific image from resources
Dim pictureResource =
My.Resources.ResourceManager.GetObject("sign" & correctAnswer)
signPicture.Image = CType(pictureResource, Image) ' display image
End Sub ' DisplaySign
Function GetUniqueRandomNumber() As Integer
Dim randomObject As New Random()
Dim randomNumber As Integer
Do ' generate random numbers until unused sign is found
randomNumber = randomObject.Next(0, used.Length)
Loop Until used(randomNumber) = False
' indicate that sign has been used
used(randomNumber) = True
Return randomNumber ' return index for new sign
End Function ' GetUniqueRandomNumber
' handles Submit Button's Click event
Private Sub submitButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles submitButton.Click
' retrieve answer from ComboBox
Dim response As String =
Convert.ToString(optionsComboBox.SelectedValue)
' verify answer
If response = options(correctAnswer) Then
feedBackLabel.Text = "Correct!"
Else
feedBackLabel.Text = "Incorrect."
End If
' inform user if test is over
If count >= 5 Then ' test is over
feedBackLabel.Text &= " Done!"
nextButton.Enabled = False
submitButton.Enabled = False
optionsComboBox.Enabled = False
Else ' test is not over
submitButton.Enabled = False
nextButton.Enabled = True
End If
End Sub ' submitButton_Click
' handles Next Sign Button's Click event
Private Sub nextButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles nextButton.Click
DisplaySign() ' display next sign
feedBackLabel.Text = String.Empty ' clear output
' change selected sign to first in ComboBox
optionsComboBox.SelectedIndex = 0
count += 1 ' update number of signs shown
submitButton.Enabled = True
nextButton.Enabled = False
End Sub ' nextButton_Click
End Class ' RoadSignTest
Download