Title: CHAPTER FIVE
1CHAPTER FIVE
- Mobile Applications Using Decision Structures
2Objectives
- 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
3Objectives
- Display a message box
- Make decisions using IfThen statements
- Make decisions using IfThenElse statements
- Make decisions using nested If statements
4Objectives
- Make decisions using logical operators
- Make decisions using Case statements
- Insert code snippets
- Test input to ensure a value is numeric
5Chapter Project
6Pervasive Devices
- Visual Studio has a built-in emulator that
displays a working Pocket PC - Pervasive devices have become important in many
business venues
7Create 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. Windows Mobile 6 - In the Templates pane, click Device Application
- Change the Name of the Smart Device application
from DeviceApplication1 to WoodCabinetEstimate.
Click the OK button
8Create a Smart Device Application
9Placing 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
10Placing Objects on the PocketPC Form Object
11Using 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 --- gt pnlWoodType
12Using the Panel Object
13Adding 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
14Adding 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
15Adding the RadioButton Objects
16Windows Application Container Objects
- For Windows applications, Visual Basic provides 5
additional container classes in addition to the
Panel class. - FlowLayoutPanel
- GroupBox
- SplitContainer
- TabControl
- TableLayoutPanel
- GroupBox is most popular
17Windows 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
18GroupBox versus Panel
19Examples using MsgBox Function
- The following examples demonstrate displaying a
message box using the VB 6 style MsgBox
Function. - Later examples will show using the .NET show
method of the MessageBox class
20MsgBox Message only
- MsgBox( Message )
- --------------------------------------------------
------------ - MsgBox(Enter the Linear Feet of the Cabinet)
21Displaying a Message Box
22MsgBox 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 )
23Displaying a Message Box
24MsgBox with Caption and Button
- MsgBox( Message, ltButton Entrygt , 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)
25Displaying a Message Box
26MsgBoxStyle Button(s) Enumeration
27MsgBox with Caption, Button, and Icon
- MsgBox( Message, ltButton Entry or Icon
Picturegt , Caption ) - --------------------------------------------------
--------------------------------------- - MsgBox(User name is missing, _
MsgBoxStyle.OKCancel or MsgBoxStyle.Critical, _
or / User Name Error) - MsgBox(User name is missing, _ 1
or 16, _ also 1 16 or 17 User
Name Error) - MsgBox(You have been disconnected, _
MsgBoxStyle.RetryCancel MsgBoxStyle.Question
, _ / or ISP) - MsgBox(You have been disconnected, _
5 32, _ also 5 32 or 37
ISP)
28Displaying a Message Box
29MsgBoxStyle Icon Enumeration
30MessageBox Class Examples
- The following examples use the show method of the
MessageBox class. - The benefit of using this is it would available
for use in C applications as well. - This is the newer preferred method
- Oddly enough, this is what was used in the
previous version of the book
31MessageBox Class Syntax
- 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
32Values Returned by a MessageBox returns a value
of type DialogResult
33Displaying a Message Box 1 Argument
The Show method takes up to 4 arguments
34Displaying a Message Box
35Displaying a Message Box 2 Arguments
36Displaying a Message Box
37Displaying a Message Box 3 Arguments
38Displaying a Message Box
39MessageBoxButtons Enumeration
40Displaying a Message Box 4 Arguments
41Displaying a Message Box
42MessageBoxIcon Enumeration
There is also the member name None for which
no icon is displayed
43Message Box IntelliSense
- In the code editing window, inside the event
handler you are coding, press CTRLSPACEBAR.
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,
44Displaying 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
45Displaying a Message Box
46Displaying a Message Box 2 arguments
47Making Decisions with Conditional Statements
- Using an IfThen 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
48If..Then..Else Statement
- 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
- -or-
- If condition Then statements Else
elsestatements - Anything in brackets ( ) is optional
49Relational Operators used to create expressions
(conditions)
50Comparing 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.
51Comparing 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, aC is a Char
52Using the IfThenElse Statement
53Using the IfThenElseIf Statement
54Nested If Statements
55Nested If Statements
56Matching 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 )
57Testing the Status of a RadioButton Object in Code
Checked is a Boolean variable. You do not need
to writeIf Me.radPine.Checked True then
58Block-Level Scope
- Scope is defined by where the variable is
declared within a program - Within an event handler, an IfThenElse
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
59Using Logical Operators
- When more than one condition is included in an
If...Then...Else statement, the conditions are
called a compound condition
60Using the And Logical Operator
61Using the Or Logical Operator
62Using the Not Logical Operator
63Other Logical Operators
AndAlso / OrElse do short-circuit boolean
evaluation
64Order of Operations for Logical Operators
65Using the Not Logical Operator
66Select Case Statement
- In some programming applications, different
operations can occur based upon the value in a
single field
67Select 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
68Select Case Statement
69Select Case Test Expressions
70Using Relational Operators in a Select Case
Statement
71Using Ranges in Select Case Statements
72Selecting 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
73Code 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
74Code Snippets
75Validating 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
76Testing 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
77Checking for a Positive Number
78Deploying 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
79Deploying the Application
80Using 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
81Closing 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
82Summary
- 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
83Summary
- Display a message box
- Make decisions using IfThen statements
- Make decisions using IfThenElse statements
- Make decisions using nested If statements
84Summary
- Make decisions using logical operators
- Make decisions using Case statements
- Insert code snippets
- Test input to ensure a value is numeric
85CHAPTER FIVE COMPLETE
- Mobile Applications Using Decision Structures