Microsoft Visual Basic 2008 CHAPTER FIVE Mobile Applications Using Decision Structures
5 Chapter 5: Mobile Applications Using Decision Structures2 Objectives ►Write programs for devices other than a personal computer ►Understand the use of handheld technology ( aka smart devices ) ►Write handheld applications for a Personal Digital Assistant ►Use the Panel object ►Place RadioButton objects in applications
5 Chapter 5: Mobile Applications Using Decision Structures3 Objectives ►Display a message box ►Make decisions using If…Then statements ►Make decisions using If…Then…Else statements ►Make decisions using nested If statements
5 Chapter 5: Mobile Applications Using Decision Structures4 Objectives ►Make decisions using logical operators ►Make decisions using Case statements ►Insert code snippets ►Test input to ensure a value is numeric
5 Chapter 5: Mobile Applications Using Decision Structures5 Chapter Project
5 Chapter 5: Mobile Applications Using Decision Structures6 Pervasive Devices ►Visual Studio has a built-in emulator that displays a “working” Pocket PC ►Pervasive devices have become important in many business venues
5 Chapter 5: Mobile Applications Using Decision Structures7 Create a Smart Device Application ►New Project ►if necessary, click the plus sign next to Smart Device in the Project types pane on the left side of the New Project dialog box ►Click Pocket PC 2003 under Smart Device in the Project types list. ►In the Templates pane, click Device Application ►Change the Name of the Smart Device application from DeviceApplication1 to WoodCabinetEstimate. Click the OK button
5 Chapter 5: Mobile Applications Using Decision Structures8 Create a Smart Device Application
5 Chapter 5: Mobile Applications Using Decision Structures9 Placing Objects on the Pocket PC Form Object ►Many of the same objects used in a Windows application can be placed on the PocketPC Form object ►You cannot resize the Form object ►The PocketPC Form object can be named in the same manner as a Windows Form object using the (Name) property in the Properties window ►Change the Text property of the PocketPC Form object from Form1 to Estimate in the same manner used for the Windows Form object
5 Chapter 5: Mobile Applications Using Decision Structures10 Placing Objects on the PocketPC Form Object
5 Chapter 5: Mobile Applications Using Decision Structures11 Using the Panel Object ►If necessary, open the Device Containers category of the Toolbox by clicking the plus sign next to the category name. ►A Panel is used as a container for other controls. In this application it will be used to group radio buttons. ►(Name) property --- > pnlWoodType
5 Chapter 5: Mobile Applications Using Decision Structures12 Using the Panel Object
5 Chapter 5: Mobile Applications Using Decision Structures13 Adding the 3 RadioButton Objects ►Drag and drop one RadioButton object from the Toolbox onto the PocketPC Form object inside the Panel object. ►Drag a second RadioButton object from the Toolbox onto the PocketPC Form object using blue snap lines to align and separate the RadioButton objects vertically ►add a third RadioButton object
5 Chapter 5: Mobile Applications Using Decision Structures14 Adding the RadioButton Objects ►Name the RadioButton objects (Name) property ►The names for the radio buttons, from top to bottom, should be: radPine radOak radCherry ►Change the Text property for each RadioButton Pine Oak Cherry
5 Chapter 5: Mobile Applications Using Decision Structures15 Adding the RadioButton Objects
5 Windows Application Container Objects ►For Windows applications, Visual Basic provides 5 additional container objects: FlowLayoutPanel GroupBox SplitContainer TabControl TableLayoutPanel ►GroupBox is most popular Chapter 5: Mobile Applications Using Decision Structures16
5 Chapter 5: Mobile Applications Using Decision Structures17 Windows Application Container Objects GroupBox controls are typically used to group radio buttons, but they are NOT available for smart devices so for a smart device use a Panel control
5 GroupBox versus Panel Chapter 5: Mobile Applications Using Decision Structures18
5 Examples using MsgBox Function ►The following examples demonstrate display a message box using the VB 6 style MsgBox Function. ►Later examples will show using the.NET show method of the MessageBox class Chapter 5: Mobile Applications Using Decision Structures19
5 MsgBox [ Message only ] ►MsgBox( “Message” ) ►MsgBox(“Enter the Linear Feet of the Cabinet”) Chapter 5: Mobile Applications Using Decision Structures20
5 Chapter 5: Mobile Applications Using Decision Structures21 Displaying a Message Box
5 MsgBox [ with Caption ] ►MsgBox( “Message”,,”Caption” ) ►MsgBox(“Enter the Linear Feet of the Cabinet” _,, _ “Error Missing a Number”) ►Note the,, to indicate an optional positional parameter has been skipped ( will use default value ) Chapter 5: Mobile Applications Using Decision Structures22
5 Chapter 5: Mobile Applications Using Decision Structures23 Displaying a Message Box
5 Chapter 5: Mobile Applications Using Decision Structures24 MsgBoxStyle Button(s) Enumeration
5 MsgBoxStyle Icon Enumeration Chapter 5: Mobile Applications Using Decision Structures25 Icon NameValueIcon PictureUsage MsgBoxStyle.Critical 16 Alerts the user to an error MsgBoxStyle.Question 32 Displays a question mark MsgBoxStyle.Exclamation 48 Alerts the user to a possible problem MsgBoxStyle.Information 64 Displays an information icon
5 MsgBox [ with Caption and Button ] ►MsgBox( “Message”,, ”Caption” ) ► ►MsgBox(“User name is missing”, _ MsgBoxStyle.OKCancel, _ “Entry Error”) ►MsgBox(“You have been disconnected”, _ MsgBoxStyle.RetryCancel, _ “ISP”) ►MsgBox(“You have been disconnected”, _ 5, _ “ISP”) Chapter 5: Mobile Applications Using Decision Structures26
5 Chapter 5: Mobile Applications Using Decision Structures27 Displaying a Message Box
5 MsgBox [ with Caption, Button, and Icon ] ►MsgBox( “Message”,, ”Caption” ) ► ►MsgBox(“User name is missing”, _ MsgBoxStyle.OKCancel or MsgBoxStyle.Critical, _ ‘or OR + “User Name Error”) ►MsgBox(“User name is missing”, _ 1 or 16, _ ‘also or 17 “User Name Error”) ►MsgBox(“You have been disconnected”, _ MsgBoxStyle.RetryCancel + MsgBoxStyle.Question, _ ‘+ OR or “ISP”) ►MsgBox(“You have been disconnected”, _ , _ ‘also or 37 “ISP”) Chapter 5: Mobile Applications Using Decision Structures28
5 Displaying a Message Box Chapter 5: Mobile Applications Using Decision Structures29
5 MsgBox [ 1 Argument ] ►MsgBox( “Message” ) ►MsgBox(“Enter the Linear Feet of the Cabinet”) Chapter 5: Mobile Applications Using Decision Structures30
5 Chapter 5: Mobile Applications Using Decision Structures31 Displaying a Message Box
5 Chapter 5: Mobile Applications Using Decision Structures32 Displaying a message box using the.NET MessageBox class 1 – 4 Arguments ( overloaded ) [common usage] Message to be displayed Caption to display in the title bar [ blank if omitted ] MessageBoxButtons to be displayed [ OK if omitted ] MessageBoxIcon to be displayed [ blank if omitted ] or MessageBoxImage to be displayed [ blank if omitted ].NET Framework V 3.0 The Show method returns a value of type DialogResult
5 Values Returned by a MessageBox [ returns a value of type DialogResult ] Chapter 5: Mobile Applications Using Decision Structures33 DialogResult Enumeration Specifies identifiers to indicate the return value of a dialog box. Member nameDescription AbortThe dialog box return value is Abort (usually sent from a button labeled Abort). CancelThe dialog box return value is Cancel (usually sent from a button labeled Cancel). IgnoreThe dialog box return value is Ignore (usually sent from a button labeled Ignore). NoThe dialog box return value is No (usually sent from a button labeled No). None Nothing is returned from the dialog box. This means that the modal dialog continues running. OKThe dialog box return value is OK (usually sent from a button labeled OK). RetryThe dialog box return value is Retry (usually sent from a button labeled Retry). YesThe dialog box return value is Yes (usually sent from a button labeled Yes).
5 Chapter 5: Mobile Applications Using Decision Structures34 Displaying a Message Box [ 1 Argument ] [ The Show method takes up to 4 arguments ]
5 Chapter 5: Mobile Applications Using Decision Structures35 Displaying a Message Box
5 Chapter 5: Mobile Applications Using Decision Structures36 Displaying a Message Box [ 2 Arguments ]
5 Chapter 5: Mobile Applications Using Decision Structures37 Displaying a Message Box
5 Chapter 5: Mobile Applications Using Decision Structures38 Displaying a Message Box [ 3 Arguments ]
5 Chapter 5: Mobile Applications Using Decision Structures39 Displaying a Message Box
5 Chapter 5: Mobile Applications Using Decision Structures40 Displaying a Message Box [ Enumeration Constants ] There is also the member name None for which no icon is displayed
5 Chapter 5: Mobile Applications Using Decision Structures41 Displaying a Message Box [ 4 Arguments ]
5 Displaying a Message Box Chapter 5: Mobile Applications Using Decision Structures42
5 Chapter 5: Mobile Applications Using Decision Structures43 Message Box IntelliSense ►In the code editing window, inside the event handler you are coding, press CTRL+SPACEBAR. IntelliSense displays the allowable entries. ►Type mes to select MessageBox in the IntelliSense list ►Type a period (. ) to insert the dot operator. IntelliSense displays a list of the allowable entries. Type s to select Show in the IntelliSense list ►Type the following text: (“You have been disconnected from the Internet”, “ISP”,
5 Chapter 5: Mobile Applications Using Decision Structures44 Displaying a Message Box ►Here are the last two arguments for the Show method: MessageBoxButtons.RetryCancel MessageBoxIcon.Warning MessageBox.Show (“You have been disconnected from the Internet”,_ “ISP”,_ MessageBoxButtons.RetryCancel,_ MessageBoxIcon.Warning) ►Type a right parenthesis and then press the ENTER key
5 Chapter 5: Mobile Applications Using Decision Structures45 Displaying a Message Box
5 Chapter 5: Mobile Applications Using Decision Structures46 Making Decisions with Conditional Statements Using an If…Then Statement ►A decision structure is one of the three fundamental control structures used in computer programming. Sequence, Selection, Repetition ►When a condition is tested in a Visual Basic program, the condition either is true or false
5 Chapter 5: Mobile Applications Using Decision Structures47 Relational Operators
5 Chapter 5: Mobile Applications Using Decision Structures48 Relational Operators ►With the insertion point located in the correct location in the code, type if and then press the SPACEBAR ►Type inta to select the variable named intAge in the IntelliSense list. Then, type >=18 as the condition to be tested. Press the ENTER key ►On the blank line, enter the statement that should be executed when the condition is true. To place the message, “You are old enough to vote” in the Text property of the lblVotingEligibility Label object, insert the code shown in Figure 5-41 on page 316. Remember to use IntelliSense to reference the lblVotingEligibility Label object
5 Relational Operators Chapter 5: Mobile Applications Using Decision Structures49
5 Chapter 5: Mobile Applications Using Decision Structures50 Comparing Strings ►A string value comparison compares each character in two strings, starting with the first character in each string
5 Chapter 5: Mobile Applications Using Decision Structures51 Comparing Different Data Types ►Every type of data available in Visual Basic can be compared Different numeric types can be compared to each other A single string character can be compared to a Char data type
5 Using the If…Then…Else Statement Chapter 5: Mobile Applications Using Decision Structures52
5 Chapter 5: Mobile Applications Using Decision Structures53 Using the If…Then…ElseIf Statement
5 Nested If Statements Chapter 5: Mobile Applications Using Decision Structures54
5 Nested If Statements Chapter 5: Mobile Applications Using Decision Structures55
5 Chapter 5: Mobile Applications Using Decision Structures56 Matching If, Else, and End If Entries ►If statements must be fully contained within the outer If statement ►Place the correct statements with the correct If and Else statements within the nested If statement This illustration shows incorrect logic
5 Testing the Status of a RadioButton Object in Code Chapter 5: Mobile Applications Using Decision Structures57
5 Chapter 5: Mobile Applications Using Decision Structures58 Block-Level Scope ►Scope is defined by where the variable is declared within a program ►Within an event handler, an If…Then…Else statement is considered a block of code ►Variables can be declared within a block of code The variable can be referenced only within the block of code where it is declared
5 Chapter 5: Mobile Applications Using Decision Structures59 Using Logical Operators ►When more than one condition is included in an If...Then...Else statement, the conditions are called a compound condition
5 Using the And Logical Operator Chapter 5: Mobile Applications Using Decision Structures60
5 Using the Or Logical Operator Chapter 5: Mobile Applications Using Decision Structures61
5 Using the Not Logical Operator Chapter 5: Mobile Applications Using Decision Structures62
5 Chapter 5: Mobile Applications Using Decision Structures63 Other Logical Operators
5 Chapter 5: Mobile Applications Using Decision Structures64 Order of Operations for Logical Operators
5 Chapter 5: Mobile Applications Using Decision Structures65 Select Case Statement ►In some programming applications, different operations can occur based upon the value in a single field
5 Select Case Statement Chapter 5: Mobile Applications Using Decision Structures66
5 Chapter 5: Mobile Applications Using Decision Structures67 Select Case Test Expressions
5 Chapter 5: Mobile Applications Using Decision Structures68 Using Relational Operators in a Select Case Statement
5 Using Ranges in Select Case Statements Chapter 5: Mobile Applications Using Decision Structures69
5 Chapter 5: Mobile Applications Using Decision Structures70 Selecting Which Decision Structure to Use ►You might be faced with determining if you should use the Select Case statement or the If...Then...ElseIf statement to solve a problem ►Generally, the Select Case statement is most useful when more than two or three values must be tested for a given variable ►The If...Then...ElseIf statement is more flexible More than one variable can be used in the comparison Compound conditions with the And, Or, and Not logical operators can be used
5 Chapter 5: Mobile Applications Using Decision Structures71 Code Snippets ►Right-click the line in the code editing window where you want to insert the snippet ►Click Insert Snippet on the shortcut menu ►Double-click Code Patterns - If, For Each,Try Catch, Property, etc, which is a folder that contains commonly used code such as the If...Then...Else statement ►Double-click the Conditionals and Loops folder because an If...Then...Else statement is a conditional statement ►Double-click the If...Else...End If Statement code snippet
5 Chapter 5: Mobile Applications Using Decision Structures72 Code Snippets
5 Chapter 5: Mobile Applications Using Decision Structures73
5 Chapter 5: Mobile Applications Using Decision Structures74 Validating Data ►Developers should anticipate that users will enter invalid data ►Developers must write code that will prevent the invalid data from being used in the program to produce invalid output
5 Chapter 5: Mobile Applications Using Decision Structures75 Testing Input to Determine If the Value Is Numeric ►The Visual Basic IsNumeric function can check the input value to determine if the value can be converted into a numeric value such as an Integer or Decimal data type
5 Chapter 5: Mobile Applications Using Decision Structures76 Checking for a Positive Number
5 Chapter 5: Mobile Applications Using Decision Structures77 Deploying the Application ►With Visual Studio open and the program you want to run loaded, click the Start Debugging button on the Standard toolbar ►If necessary, select USA Windows Mobile 5.0 Pocket PC R2 Emulator in the Device list. Click the Deploy button ►After the Wood Cabinet Estimate application loads and executes on the emulator device, type 15 in the Linear Feet text box ►Using your mouse, click the Oak radio button, and then click the Calculate button
5 Chapter 5: Mobile Applications Using Decision Structures78 Deploying the Application
5 Chapter 5: Mobile Applications Using Decision Structures79
5 Chapter 5: Mobile Applications Using Decision Structures80 Using the Input Panel ►When you use the emulator, you can enter data directly from the keyboard ►The Pocket PC has the input panel to enter data into applications You can use a stylus to select the characters from the input panel. When you press the stylus on a character in the input panel, the character is entered into the focused object on the form
5 Chapter 5: Mobile Applications Using Decision Structures81 Closing the Emulator ►When you are finished with the application, close the emulator by clicking the Close button (X) in the upper-right corner of the Pocket PC emulator ►It is critical that you click the No button
5 Chapter 5: Mobile Applications Using Decision Structures82 Summary ►Write programs for devices other than a personal computer ►Understand the use of handheld technology ►Write handheld applications for a Personal Digital Assistant ►Use the Panel object ►Place RadioButton objects in applications
5 Chapter 5: Mobile Applications Using Decision Structures83 Summary ►Display a message box ►Make decisions using If…Then statements ►Make decisions using If…Then…Else statements ►Make decisions using nested If statements
5 Chapter 5: Mobile Applications Using Decision Structures84 Summary ►Make decisions using logical operators ►Make decisions using Case statements ►Insert code snippets ►Test input to ensure a value is numeric
Microsoft Visual Basic 2008 CHAPTER FIVE COMPLETE Mobile Applications Using Decision Structures