1 / 15

Chapter 6

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

les
Télécharger la présentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Arrays, Random, Functions, Subroutines

  2. Application #1

  3. Global and Form loads PublicClassEnhancedCafeteriaSurvey ' two-dimensional array stores voting results Dim votes(3, 1) AsInteger ' handles Form's Load event PrivateSubEnhancedCafeteriaSurvey_Load(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) HandlesMyBase.Load foodsComboBox.SelectedIndex = 0 ' select first food in list EndSub' EnhancedCafeteriaSurvey_Load

  4. ' handles Vote Button's Click event PrivateSubvoteButton_Click(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) HandlesvoteButton.Click Dim index AsInteger = foodsComboBox.SelectedIndex ' count the vote IflikeRadioButton.Checked = TrueThen votes(index, 0) += 1 Else votes(index, 1) += 1 EndIf DisplayVotes() ' display the voting results EndSub' voteButton_Click ' display the voting results in the ListBox SubDisplayVotes() 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 AsInteger = 0 Tovotes.GetUpperBound(0) resultsListBox.Items.Add( foodsComboBox.Items(counter).ToString() & vbTab & votes(counter, 0) & vbTab & votes(counter, 1)) Next EndSub' DisplayVotes Function call

  5. ' display the voting results in the ListBox SubDisplayVotes() 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 AsInteger = 0 Tovotes.GetUpperBound(0) resultsListBox.Items.Add( foodsComboBox.Items(counter).ToString() & vbTab & votes(counter, 0) & vbTab & votes(counter, 1)) Next EndSub' DisplayVotes

  6. Application #2

  7. PublicClassGuessTheNumber DimrandomObjectAsNewRandom() Dim number AsInteger = randomObject.Next(1, 101) ' handles Enter button click event PrivateSubenterButton_Click(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) HandlesenterButton.Click ' retrieve the user's guess Dim guess AsInteger = 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..." EndIf guessTextBox.Focus() ' give focus to the TextBox EndSub' enterButton_Click

  8. PrivateSubnewGameButton_Click(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) HandlesnewGameButton.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() EndSub' newGameButton_Click ' handles Guess TextBox'sTextChanged event PrivateSubguessTextBox_TextChanged(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) HandlesguessTextBox.TextChanged outputLabel.Text = String.Empty' clear result EndSub' guessTextBox_TextChanged

  9. PublicClassRoadSignTest ' String array stores sign names Dim options() AsString = { "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)) AsBoolean Dim count AsInteger = 1 ' number of signs shown DimcorrectAnswerAsInteger' index of current sign ' handles Road Sign Test Form's Load event PrivateSubRoadSignTest_Load(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) HandlesMyBase.Load Array.Sort(options) ' alphabetize sign names ' display sign names in ComboBox optionsComboBox.DataSource = options DisplaySign() ' display first sign in PictureBox EndSub' RoadSignTest_Load

  10. ' display random sign in PictureBox SubDisplaySign() ' unique index ensures that a sign is used no more than once correctAnswer = GetUniqueRandomNumber() ' retrieve specific image from resources DimpictureResource = My.Resources.ResourceManager.GetObject("sign" & correctAnswer) signPicture.Image = CType(pictureResource, Image) ' display image EndSub' DisplaySign FunctionGetUniqueRandomNumber() AsInteger DimrandomObjectAsNewRandom() DimrandomNumberAsInteger Do' generate random numbers until unused sign is found randomNumber = randomObject.Next(0, used.Length) LoopUntil used(randomNumber) = False ' indicate that sign has been used used(randomNumber) = True ReturnrandomNumber' return index for new sign EndFunction' GetUniqueRandomNumber

  11. ' handles Submit Button's Click event PrivateSubsubmitButton_Click(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) HandlessubmitButton.Click ' retrieve answer from ComboBox Dim response AsString = Convert.ToString(optionsComboBox.SelectedValue) ' verify answer If response = options(correctAnswer) Then feedBackLabel.Text = "Correct!" Else feedBackLabel.Text = "Incorrect." EndIf ' 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 EndIf EndSub' submitButton_Click

  12. ' handles Next Sign Button's Click event PrivateSubnextButton_Click(ByVal sender AsSystem.Object, ByVal e AsSystem.EventArgs) HandlesnextButton.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 EndSub' nextButton_Click EndClass' RoadSignTest

More Related