Title: Using Menus, Common Dialogs, Procedures, Functions, and Arrays
1Chapter 7
- Using Menus, Common Dialogs, Procedures,
Functions, and Arrays
2Objectives
- Understand menu bar, menus, shortcut menu,
- Understand the StatusBar, the PictureBox
controls, - Use one-dimensional and multidimensional arrays
in code - Passing arguments to functions and procedures.
3Know You Dog Quiz
- The application displays a picture of a specific
dog breed which the user must identify. User may
navigate to the first, last, previous, or next
question. To pass the quiz, the user must answer
all questions correctly.
4InterfaceDesign
5Interface Design
- A shortcut menu provides users with an easily
accessible menu of commands, given the context of
the application - A ContextMenu control is used to add the shortcut
menu to the form - Use a common dialog box when a shortcut is used
- A PictureBox control displays the image in
different formats. - A StatusBar control anchors to the bottom of the
form, displays status information.
6Array
- An Array holds many values. It provides a way to
give several variables a common name and use an
index to access each item. - Examples
- Solution(1) 1 Solution(2) 4
- QuestionChoice(1,1) English Springer Spaniel
- QuestionChoice(1,2) Akita
- QuestionChoice(1,3) Husky
7Arrays
- Allows programmers to represent many values with
one variable name - Elements in the array are distinguished by their
subscript - The lower-bound of a subscript is 0
For intNumber 0 To 11 dblTotal
dblMonth(intNumber) Next intNumber
8The Dim Statement for Arrays
- Dynamic dimension A variable or expression for
upper-bound value - Dim intValues(intUpperBound) As Integer
9Declaring Arrays
- The program uses gshrAnswer array to track the
users answers. - All values of array are initialized to -1
- If the user licks the second RadioButton, the
corresponding element receives a value of 1
10Declaring Arrays
- The solution to the quiz questions is stored in
gshrSolution array, and compared to gshrAnswers
array. - An element with a value of 0 indicates that the
correct answer should be the answer displayed on
the first RadioButton control.
11Indexes
- Elements in an array are referenced in code by
their index
12Multidimensional Arrays
- VB. Net allows arrays to have up to 32
dimensions. - Dim dblCost(2, 4) As Double
13Multidimensional Arrays
Dim intTime() as Short _ 13, 30, 5, 17,
12, 45, _ 34, 26, 16, 26, 23, 35,
IntSum2 0 IntSum4 0 For intModel 0 TO 4
intSum2 intTime(intModel, 1) Next
intModel For intMachine 0 TO 5 intSum3
intTime(3, intMachine) Next intMachine
14Declaring a Two-Dimensional Array
- Quiz has seven questions, each of which has four
answer choices and one image file - We use an array of 7 by 5.
- Each question is represented by a row, with each
answer choice represented by a column - The image file name is represented by the last
column
15Array Methods
- You may use several array methods to retrieve
information about an array
16Sub Procedures
- A sub procedure is a unit of code that executes
when called from code in other procedures - Unlike function procedures, sub procedures do not
return values - Like functions, sub procedures accept variables,
constants and expressions as arguments
17Function Procedures
- Performs tasks and returns a value back to the
code that called it - Similar to VB.NETs intrinsic functions
18Returning a Value by a Function
- You return a value from a function procedure to
the line of code that called it by - Setting the function name to a value within the
function before exiting. - Using the return statement
19Function QuizResult()
- The QuizResult() function returns True if the
user answers all of the questions correctly.
20Exit Menu Command and FontDialog Control
- Click the Form1.vb tab in the main work area.
Double-click the Exit command on the File menu of
the Form1 form and type line 345 in the code
window - Double-click the Font command on the Options
menu, and type lines 349 through 355
21Passing Arguments Between Procedures
- Passing By Value method
- ByVal keyword
- Passes the value of the variable (default method)
- CalculateTax((dblIncome),intTaxBracket)
- Passing By Reference method
- ByRef keyword
- Passes the memory location of the variable
- Allows you to modify the variable in procedure
- Private Sub CalculateTax(ByRef dblValue As Double)
22Coding a Sub Procedure
- A sub procedure DisplayQuestion() displays a
question to user and checks the radio buttons for
the answer
23Coding a Sub Procedure
- The value of the radAnswer1.Checked property is
compared with True.