Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures 5 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 Chapter 5: Mobile Applications Using Decision Structures 2 5 Objectives ►Display a message box ►Make decisions using If…Then statements ►Make decisions using If…Then…Else statements ►Make decisions using nested If statements Chapter 5: Mobile Applications Using Decision Structures 3 5 Objectives ►Make decisions using logical operators ►Make decisions using Case statements ►Insert code snippets ►Test input to ensure a value is numeric Chapter 5: Mobile Applications Using Decision Structures 4 5 Chapter Project Chapter 5: Mobile Applications Using Decision Structures 5 5 Pervasive Devices ►Visual Studio has a built-in emulator that displays a “working” Pocket PC ►Pervasive devices have become important in many business venues Chapter 5: Mobile Applications Using Decision Structures 6 5 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 Chapter 5: Mobile Applications Using Decision Structures 7 5 Create a Smart Device Application Chapter 5: Mobile Applications Using Decision Structures 8 5 Placing Objects on the PocketPC 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 Chapter 5: Mobile Applications Using Decision Structures 9 5 Placing Objects on the PocketPC Form Object Chapter 5: Mobile Applications Using Decision Structures 10 5 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 Chapter 5: Mobile Applications Using Decision Structures 11 5 Using the Panel Object Chapter 5: Mobile Applications Using Decision Structures 12 5 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 Chapter 5: Mobile Applications Using Decision Structures 13 5 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 Chapter 5: Mobile Applications Using Decision Structures 14 5 Adding the RadioButton Objects Chapter 5: Mobile Applications Using Decision Structures 15 5 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 Chapter 5: Mobile Applications Using Decision Structures 16 5 GroupBox versus Panel Chapter 5: Mobile Applications Using Decision Structures 17 5 Displaying a Message Box 1 – 4 Arguments ( overloaded ) • Message to be displayed • Caption to display in the title bar [ blank if omitted ] • MessageBoxButton to be displayed [ OK if omitted • MessageBoxIcon to be displayed [ blank if omitted ] ] The Show method returns a value of type DialogResult Chapter 5: Mobile Applications Using Decision Structures 18 5 Values Returned by a MessageBox [ returns a value of type DialogResult ] DialogResult Enumeration Specifies identifiers to indicate the return value of a dialog box. Member name Abort Description The dialog box return value is Abort (usually sent from a button labeled Abort). Cancel The dialog box return value is Cancel (usually sent from a button labeled Cancel). Ignore The dialog box return value is Ignore (usually sent from a button labeled Ignore). No None OK Retry Yes The dialog box return value is No (usually sent from a button labeled No). Nothing is returned from the dialog box. This means that the modal dialog continues running. The dialog box return value is OK (usually sent from a button labeled OK). The dialog box return value is Retry (usually sent from a button labeled Retry). The dialog box return value is Yes (usually sent from a button labeled Yes). Chapter 5: Mobile Applications Using Decision Structures 19 5 Displaying a Message Box [ 1 Argument ] [ The Show method takes up to 4 arguments ] Chapter 5: Mobile Applications Using Decision Structures 20 5 Displaying a Message Box [ 2 Arguments ] Chapter 5: Mobile Applications Using Decision Structures 21 5 Displaying a Message Box [ 3 Arguments ] Chapter 5: Mobile Applications Using Decision Structures 22 5 Displaying a MessageBox MessageBoxButtons Specifies constants defining which buttons to display on a MessageBox. These are defined as an enumeration Member name Description OK The message box contains an OK button. [ default ] OKCancel The message box contains OK and Cancel buttons. AbortRetryIgnore YesNoCancel YesNo RetryCancel The message box contains Abort, Retry, and Ignore buttons. The message box contains Yes, No, and Cancel buttons. The message box contains Yes and No buttons. The message box contains Retry and Cancel buttons. Chapter 5: Mobile Applications Using Decision Structures 23 5 Displaying a Message Box [ This is for a MsgBox Function… ] Chapter 5: Mobile Applications Using Decision Structures 24 5 Displaying a Message Box [ 4 Arguments ] Chapter 5: Mobile Applications Using Decision Structures 25 5 Displaying a Message Box [ Enumeration Constants ] There is also the member name None for which no icon is displayed Chapter 5: Mobile Applications Using Decision Structures 26 5 Displaying a Message Box ►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”) Chapter 5: Mobile Applications Using Decision Structures 27 5 Displaying a Message Box ►Here are the last two arguments for the Show method: • MessageBoxButtons.RetryCancel • MessageBoxIcon.Warning ►Type a right parenthesis and then press the ENTER key Chapter 5: Mobile Applications Using Decision Structures 28 5 Displaying a Message Box Chapter 5: Mobile Applications Using Decision Structures 29 5 Displaying a Message Box [ 2 arguments ] Chapter 5: Mobile Applications Using Decision Structures 30 5 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 Chapter 5: Mobile Applications Using Decision Structures 31 5 If...Then...Else Statement Conditionally executes a group of statements, depending on the value of an expression. If condition [ Then ] [ statements ] [ ElseIf elseifcondition [ Then ] [ elseifstatements ] ] [ Else [ elsestatements ] ] End If -orIf condition Then [ statements ] [ Else elsestatements ] Anything in brackets ( [ ] ) is optional Chapter 5: Mobile Applications Using Decision Structures 32 5 Relational Operators [ used to create expressions (conditions) ] Chapter 5: Mobile Applications Using Decision Structures 33 5 Relational Operators Characters are compared using their ordinal value in the Unicode Character Set. Chapter 5: Mobile Applications Using Decision Structures 34 5 Comparing Strings ► A string value comparison compares each character in two strings, starting with the first character in each string ( using ordinal values in the Unicode Character Set ) If the strings are not the same size, the shorter string is padded on the right with spaces to make it the same length. Note: spaces come earlier in the collating sequence than do letters and numbers. Chapter 5: Mobile Applications Using Decision Structures 35 5 Comparing Different Data Types ►Every type of data available in Visual Basic can be compared • Different numeric types can be compared to each other [ numerically ] • A single string character can be compared to a Char data type [ using ordinal values ] • If comparing a string to a character literal and option strict is on, you must use the type character C. “a” is a String, “a”C is a Char… Chapter 5: Mobile Applications Using Decision Structures 36 5 Using the If…Then…Else Statement Chapter 5: Mobile Applications Using Decision Structures 37 5 Using the If…Then…ElseIf Statement Notice the use of the type character D making the Double literals type Decimal Chapter 5: Mobile Applications Using Decision Structures 38 5 Nested If Statements Chapter 5: Mobile Applications Using Decision Structures 39 5 Nested If Statements Chapter 5: Mobile Applications Using Decision Structures 40 5 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 ( this is a LOGIC error, not a SYNTAX error ) Chapter 5: Mobile Applications Using Decision Structures 41 5 Testing the Status of a RadioButton Object in Code Checked is a Boolean variable. You do not need to write: If Me.radPine.Checked = True then Chapter 5: Mobile Applications Using Decision Structures 42 5 Block-Level Scope ► Scope is determined by where the variable is accessible 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 Chapter 5: Mobile Applications Using Decision Structures 43 5 Using Logical Operators ►When more than one condition is included in an If...Then...Else statement, the conditions are called a compound condition Chapter 5: Mobile Applications Using Decision Structures 44 5 Using the And Logical Operator Chapter 5: Mobile Applications Using Decision Structures 45 5 Using the Or Logical Operator Chapter 5: Mobile Applications Using Decision Structures 46 5 Using the Not Logical Operator Chapter 5: Mobile Applications Using Decision Structures 47 5 Other Logical Operators Chapter 5: Mobile Applications Using Decision Structures 48 5 Order of Operations for Logical Operators Chapter 5: Mobile Applications Using Decision Structures 49 5 Select Case Statement ►In some programming applications, different operations can occur based upon the value in a single field Chapter 5: Mobile Applications Using Decision Structures 50 5 Select Case Statement Select...Case Statement (Visual Basic) Runs one of several groups of statements, depending on the value of an expression. Select [ Case ] testexpression [ Case expressionlist [ statements ] ] [ Case Else [ elsestatements ] ] End Select Chapter 5: Mobile Applications Using Decision Structures 51 5 Select Case Statement Chapter 5: Mobile Applications Using Decision Structures 52 5 Select Case Statement Chapter 5: Mobile Applications Using Decision Structures 53 5 Select Case Test Expressions Chapter 5: Mobile Applications Using Decision Structures 54 5 Using Relational Operators in a Select Case Statement Chapter 5: Mobile Applications Using Decision Structures 55 5 Using Ranges in Select Case Statements Chapter 5: Mobile Applications Using Decision Structures 56 5 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 Chapter 5: Mobile Applications Using Decision Structures 57 5 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 Common Code Patterns, 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 Chapter 5: Mobile Applications Using Decision Structures 58 5 Code Snippets Chapter 5: Mobile Applications Using Decision Structures 59 5 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 Visual Basic Language Reference IsNumeric Function (Visual Basic) Returns a Boolean value indicating whether an expression can be evaluated as a number. Public Function IsNumeric(ByVal Expression As Object) As Boolean Chapter 5: Mobile Applications Using Decision Structures 60 5 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 Chapter 5: Mobile Applications Using Decision Structures 61 5 Checking for a Positive Number Chapter 5: Mobile Applications Using Decision Structures 62 5 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 Pocket PC 2003 SE 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 Chapter 5: Mobile Applications Using Decision Structures 63 5 Deploying the Application Chapter 5: Mobile Applications Using Decision Structures 64 5 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 Chapter 5: Mobile Applications Using Decision Structures 65 5 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 Chapter 5: Mobile Applications Using Decision Structures 66 5 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 Chapter 5: Mobile Applications Using Decision Structures 67 5 Summary ►Display a message box ►Make decisions using If…Then statements ►Make decisions using If…Then…Else statements ►Make decisions using nested If statements Chapter 5: Mobile Applications Using Decision Structures 68 5 Summary ►Make decisions using logical operators ►Make decisions using Case statements ►Insert code snippets ►Test input to ensure a value is numeric Chapter 5: Mobile Applications Using Decision Structures 69 Microsoft Visual Basic 2005 CHAPTER 5 COMPLETE Mobile Applications Using Decision Structures
© Copyright 2026 Paperzz