1 / 56

Tutorial 17 – Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es

Tutorial 17 – Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es. Outline 17.1 Test-Driving the Flag Quiz Application 17.2 Introducing Arrays 17.3 Declaring and Allocating Arrays 17.4 Constructing the Flag Quiz Application 17.5 Sorting Arrays 17.6 Wrap-Up.

Download Presentation

Tutorial 17 – Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es

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. Tutorial 17 – Flag Quiz ApplicationIntroducing One-Dimensional Arrays and ComboBoxes Outline17.1 Test-Driving the Flag Quiz Application17.2 Introducing Arrays17.3 Declaring and Allocating Arrays17.4 Constructing the Flag Quiz Application17.5 Sorting Arrays17.6 Wrap-Up

  2. Objectives • In this tutorial, you will learn to: • Create and initialize arrays. • Store information in an array. • Refer to individual elements of an array. • Sort arrays. • Use ComboBoxes to display options in a drop-down list. • Determine whether a specific character is in a String. • Remove a character from a String. • Convert a String to lowercase characters.

  3. 17.1 Test-Driving the Flag Quiz Application

  4. PictureBoxdisplays flag 17.1 Test-Driving the Flag Quiz Application Figure 17.1 Flag Quiz application’s Form. • Load the Flag Quiz application • Debug > Start • Note that a different flag from the one in Fig. 17.1 may appear

  5. Answer being selected 17.1 Test-Driving the Flag Quiz Application Figure 17.2 Selecting an answer from the ComboBox.

  6. 17.1 Test-Driving the Flag Quiz Application Figure 17.3 Submitting the correct answer.

  7. 17.1 Test-Driving the Flag Quiz Application Figure 17.4 Displaying the next flag.

  8. 17.1 Test-Driving the Flag Quiz Application Figure 17.5 Submitting an incorrect answer.

  9. ComboBoxis disabled when the quiz ends 17.1 Test-Driving the Flag Quiz Application Figure 17.6 Finishing the quiz. • Program ends after 5 questions • Buttons and ComboBox will be disabled

  10. 17.2 Introducing Arrays • Each array contains data using same variable name and data type • Each value or object stored at a position • Position numbers start at zero • Zeroth element is first element • Position also called index or subscript • Index must be zero, a positive Integer, or an Integer expression

  11. 17.2 Introducing Arrays Figure 17.7 Array consisting of 13 elements 0 intNetUnitsSold(0) Name of array (note that all elements of this array 10 intNetUnitsSold(1) have the same name, intNetUnitsSold ) 16 intNetUnitsSold(2) 72 intNetUnitsSold(3) 154 intNetUnitsSold(4) 89 intNetUnitsSold(5) intNetUnitsSold(6) 0 intNetUnitsSold(7) 62 intNetUnitsSold(8) -3 intNetUnitsSold(9) 90 453 intNetUnitsSold(10) Position number (index or subscript) of the 178 intNetUnitsSold(11) element within array intNetUnitsSold 78 intNetUnitsSold(12)

  12. 17.3 Declaring and Allocating Arrays Figure 17.8 Sum Array application’s Form in design view.

  13. 17.3 Declaring and Allocating Arrays • You must specify the size, name and data type for an array before using • Allocating memory for the array • Use New to create the array • Initializer list • Specifies initial values of elements

  14. Creating an array ofIntegers 17.3 Declaring and Allocating Arrays Figure 17.9 Declaring an array in the event handler.

  15. 17.3 Declaring and Allocating Arrays • GetUpperBound method • Returns highest index in the array • Length property • Returns the length of the array

  16. Retrieve the value of each element and add it to the total, one at a time 17.3 Declaring and Allocating Arrays Figure 17.10 Calculating the sum of the values of an array’s elements.

  17. 17.3 Declaring and Allocating Arrays Figure 17.11 Displaying the sum of the values of an array’s elements.

  18. Action Control/Class/Object Event Label the application’s controls , fraFlagGroupBox lblChoose FrmFlagQuiz Load Sort the countries alphabetically Array Place countries in the ComboBox cboOptions Randomly select a flag objRandom Display the flag p icFlag btnSubmit Click Retrieve the selected country cboOptions If selected value matches the correct lblFeedback answer Display "Correct!" in the Label Else lblFeedback Display "Sorry, incorrect." in Label If five images have been displayed lblFeedback A ppend "Done!" to Label’s text Disable the Buttons and , btnNext , btnSubmit ComboBox cboOptions Flag Quiz Figure 17.12 application’s ACE table. 17.4 Constructing the Flag Quiz Application

  19. 17.4 Constructing the Flag Quiz Application

  20. 17.4 Constructing the Flag Quiz Application • Declaring the array of country names • Each element is a String containing the name of a country • Compiler allocates the size of the array to hold the number of items in the list

  21. Creating an array ofStringsto store country names 17.4 Constructing the Flag Quiz Application Figure 17.13 String array that stores country names.

  22. 17.4 Constructing the Flag Quiz Application • Creating and using a Boolean array • Game should not display a flag more than once • Random-number generation could cause a flag to be repeated • Use a Boolean array to keep track of which flags have been displayed

  23. Creating an array ofBooleanvalues with the same number of elements as the array of country names 17.4 Constructing the Flag Quiz Application Figure 17.14 Boolean array that keeps track of displayed flags.

  24. 17.4 Constructing the Flag Quiz Application • Instance variables used in the Flag Quiz application • Counter (m_intCount) • String variable to store answer (m_strCountry)

  25. Creating instance variables 17.4 Constructing the Flag Quiz Application Figure 17.15 Instance variables, used throughout the application.

  26. 17.4 Constructing the Flag Quiz Application Figure 17.16 Flag Quiz template application’s Form.

  27. 17.4 Constructing the Flag Quiz Application • ComboBox control • A combination of TextBox and ListBox • Allows you to select items from a list and/or enter your own text as input

  28. 17.4 Constructing the Flag Quiz Application • ComboBox properties • DropDownStyle property • Determines ComboBox’s appearance • MaxDropDownItems property • Determines how many items can be displayed in the drop-down list

  29. Newly created ComboBox 17.4 Constructing the Flag Quiz Application Figure 17.17 ComboBox added to Flag Quiz application’s Form.

  30. 17.4 Constructing the Flag Quiz Application • Displaying items in the ComboBox • DataSource specifies source of ComboBox’s contents • You will usem_StrOptions

  31. Specifying the source of theComboBoxitems 17.4 Constructing the Flag Quiz Application Figure 17.18 Assigning the String elements of an array to a ComboBox.

  32. 17.4 Constructing the Flag Quiz Application • Removing whitespace • Method IndexOf • Returns index of the specified character in the String • If character does not appear, returns –1 • Remove method • Takes an index and the number of characters to remove as arguments • Removes specified number of characters starting at specified index • Operates on a copy of the String

  33. Removing whitespace from country names 17.4 Constructing the Flag Quiz Application Figure 17.19 Removing whitespace from the country name.

  34. 17.4 Constructing the Flag Quiz Application • Building a flag-image file’s path name • Use the ToLower method to ensure all letters are lowercase • Append extension to strOutput (.png) • Add the fully qualified path name • Insert adds the CurrentDirectory and “/images/” to the file name

  35. Constructing full path name for flag images Returning full path name of flag images 17.4 Constructing the Flag Quiz Application Figure 17.20 Building the flag-image file path name.

  36. 17.4 Constructing the Flag Quiz Application • Selecting a unique flag to display • Generate a random index between 0 andm_blnUsed.Length • If index has been selected before, value will be True • Repeat above steps until a False value is found

  37. Determining if a country’s flag has been displayed previously 17.4 Constructing the Flag Quiz Application Figure 17.21 Generating a unique index.

  38. 17.4 Constructing the Flag Quiz Application • Returning the index of a value containing False • Set element at selected index of m_blnUsed to True • Return index

  39. Indicating that unused flag will be displayed and return the flag’s index for use 17.4 Constructing the Flag Quiz Application Figure 17.22 Returning the unique index.

  40. Getting index of unused flag Retrieving the flag’s corresponding country name 17.4 Constructing the Flag Quiz Application Figure 17.23 Choosing a random country name.

  41. 17.4 Constructing the Flag Quiz Application • Displaying a flag • Obtain unique index • Retrieve country name • Build path name • Set PictureBox picFlag’s Image property to the Image object returned by method Image.FromFile Method

  42. Getting the path name of the flag and displaying the flag image 17.4 Constructing the Flag Quiz Application Figure 17.24 Displaying a flag image.

  43. Displaying a flag when application is first run 17.4 Constructing the Flag Quiz Application Figure 17.25 Displaying a flag when the Form is loaded.

  44. 17.4 Constructing the Flag Quiz Application • Processing a user’s answer • Determine whether user’s response matches correct answer

  45. Retrieving user’s answer Determining if user’s answer is correct 17.4 Constructing the Flag Quiz Application Figure 17.26 SubmitButtonClick event handler.

  46. 17.4 Constructing the Flag Quiz Application • Determine whether the quiz is over • If 5 flags have been displayed • Disable Buttons • Display text informing user that the quiz is over • Otherwise continue quiz • Disable SubmitButton

  47. Determining if quiz is over 17.4 Constructing the Flag Quiz Application Figure 17.27 Testing whether the quiz is finished.

  48. 17.4 Constructing the Flag Quiz Application • Displaying the next flag • Call DisplayFlag to place the next flag in the PictureBox • Clear previous results

  49. Displaying the next flag for the user to identify 17.4 Constructing the Flag Quiz Application Figure 17.28 NextFlagButtonClick event handler.

  50. 17.5 Sorting Arrays • Sorting the array of country names • Array.Sort method • Returns array in ascending alphabetical order

More Related