Download presentation
Presentation is loading. Please wait.
Published byRalf Hampton Modified over 8 years ago
1
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 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
2
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 ComboBox es 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
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 17.1 Test-Driving the Flag Quiz Application
4
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 17.1 Test-Driving the Flag Quiz Application Figure 17.1 Flag Quiz application’s Form. PictureBox displays flag Load the Flag Quiz application –Debug > Start –Note that a different flag from the one in Fig. 17.1 may appear ComboBox contains answers (country names)
5
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 17.1 Test-Driving the Flag Quiz Application Figure 17.2 Selecting an answer from the ComboBox. Answer being selected Scrollbar in ComboBox ’s drop-down list
6
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 17.1 Test-Driving the Flag Quiz Application Figure 17.3 Submitting the correct answer.
7
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 17.1 Test-Driving the Flag Quiz Application Figure 17.4 Displaying the next flag.
8
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 17.1 Test-Driving the Flag Quiz Application Figure 17.5 Submitting an incorrect answer.
9
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 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 ComboBox is disabled when the quiz ends
10
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 17.2Introducing Arrays Group of memory locations Contain 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 int, or an int expression
11
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 17.2 Introducing Arrays Figure 17.7 Array consisting of 13 elements
12
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 17.3 Declaring and Allocating Arrays Figure 17.8 Sum Array application’s Form in design view.
13
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 17.3 Declaring and Allocating Arrays Figure 17.9 Declaring an array in the event handler. Creating an array of int s
15
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 17.3 Declaring and Allocating Arrays GetUpperBound method –Returns highest index in the array Length property –Returns the length of the array
16
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 17.3 Declaring and Allocating Arrays Figure 17.10 Calculating the sum of the values of an array’s elements. Retrieve the value of each element and add it to the total, one at a time
17
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 17.3 Declaring and Allocating Arrays Figure 17.11 Displaying the sum of the values of an array’s elements.
18
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 17.4 Constructing the Flag Quiz Application
19
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 17.4 Constructing the Flag Quiz Application
20
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 17.4 Constructing the Flag Quiz Application Figure 17.13 string array that stores country names. Creating an array of string s to store country names
22
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 17.4 Constructing the Flag Quiz Application Creating and using a bool array –Game should not display a flag more than once –Random-number generation could cause a flag to be repeated –Use a bool array to keep track of which flags have been displayed
23
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 17.4 Constructing the Flag Quiz Application Figure 17.14 bool array that keeps track of displayed flags. Creating an array of bool values with the same number of elements as the array of country names
24
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 17.4 Constructing the Flag Quiz Application Figure 17.15 Instance variables, used throughout the application. Creating instance variables
26
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 17.4 Constructing the Flag Quiz Application Figure 17.16 Flag Quiz template application’s Form.
27
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 17.4 Constructing the Flag Quiz Application Figure 17.17 ComboBox added to Flag Quiz application’s Form.
30
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 17.4 Constructing the Flag Quiz Application Figure 17.18 Rearranging and commenting the new control declaration.
31
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 17.4 Constructing the Flag Quiz Application Displaying items in the ComboBox –DataSource specifies source of ComboBox ’s contents You will use m_StrOptions
32
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 17.4 Constructing the Flag Quiz Application Figure 17.19 Assigning the string elements of an array to a ComboBox. Specifying the source of the ComboBox items
33
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 17.4 Constructing the Flag Quiz Application Removing whitespace –Method IndexOf Returns index of the specified character in the s tring 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
34
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 17.4 Constructing the Flag Quiz Application Figure 17.20 Removing whitespace from the country name. Removing whitespace from country names
35
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 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
36
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 17.4 Constructing the Flag Quiz Application Figure 17.21 Building the flag-image file path name. Constructing full path name for flag images Returning full path name of flag images
37
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 17.4 Constructing the Flag Quiz Application Selecting a unique flag to display –Generate a random index between 0 and m_blnUsed.Length –If index has been selected before, value will be true –Repeat above steps until a false value is found
38
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 17.4 Constructing the Flag Quiz Application Figure 17.22 Generating a unique index. Determining if a country’s flag has been displayed previously
39
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 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
40
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 17.4 Constructing the Flag Quiz Application Figure 17.23 Returning the unique index. Indicating that flag will be used and returning the flag’s index for use
41
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 17.4 Constructing the Flag Quiz Application Figure 17.24 Choosing a random country name. Getting index of unused flag Retrieving the flag’s corresponding country name
42
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 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
43
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 17.4 Constructing the Flag Quiz Application Figure 17.25 Displaying a flag image. Getting the path name of the flag and displaying the flag image
44
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 17.4 Constructing the Flag Quiz Application Figure 17.26 Displaying a flag when the Form is loaded. Displaying a flag when application is first run
45
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 45 17.4 Constructing the Flag Quiz Application Processing a user’s answer –Determine whether user’s response matches correct answer
46
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 46 17.4 Constructing the Flag Quiz Application Figure 17.27 Submit Button Click event handler. Retrieving user’s answer Determining if user’s answer is correct
47
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 47 17.4 Constructing the Flag Quiz Application Determine whether the quiz is over –If 5 flags have been displayed Disable Button s Display text informing user that the quiz is over –Otherwise continue quiz Disable Submit Button
48
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 48 17.4 Constructing the Flag Quiz Application Figure 17.28 Testing whether the quiz is finished. Determining if quiz is over
49
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 49 17.4 Constructing the Flag Quiz Application Displaying the next flag –Call DisplayFlag to place the next flag in the PictureBox –Clear previous results
50
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 50 17.4 Constructing the Flag Quiz Application Figure 17.29 Next Flag Button Click event handler. Displaying the next flag for the user to identify
51
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 51 17.5 Sorting Arrays Sorting the array of country names –Array.Sort method Returns array in ascending alphabetical order
52
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 52 17.5 Sorting Arrays Figure 17.30 Sorting the array of country names. Alphabetizing country names in the array
53
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 53 FlagQuiz.cs (1 of 9)
54
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 54 FlagQuiz.cs (2 of 9) Declaring an creating an array with an initializer list Declaring an array
55
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 55 FlagQuiz.cs (3 of 9)
56
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 56 FlagQuiz.cs (4 of 9) Sorting an array Displaying array elements in a ComboBox Creating a bool array with the new operator
57
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 57 FlagQuiz.cs (5 of 9) Locating a space character in a string Removing a character from a string Converting a string to lowercase Inserting characters into a string
58
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 58 FlagQuiz.cs (6 of 9) Assigning a value to an array element
59
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 59 FlagQuiz.cs (7 of 9) Converting the selected value from the ComboBox into a string Retrieving a value from an array
60
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 60 FlagQuiz.cs (8 of 9) Disabling the ComboBox
61
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 61 FlagQuiz.cs (9 of 9) Setting the selected ComboBox item
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.