i would like you to develop a mobile application that calculates for me the discount on an item i...

48
IF…then & vb

Upload: darren-richardson

Post on 26-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

IF…then & vb

Page 2: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Today’s ProjectI would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:

User enters in a number for the price of the item

User clicks on a radio button to select the type of discount

Output: Discounted price of the item

Page 3: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Can Visual Studio Handle This?

Absolutely!Open up Visual StudioClick the New Project button on the Standard toolbar and click Smart Device in the Project types paneClick Smart Device Project in the Templates paneChange the name from the default to DiscountCalculatorClick the Target platform list button and select Windows Mobile 5.0 Pocket PC SDK. Click Device Application and then click the OK button

Page 4: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Pocket PC Design EnvironmentCan use many of the same objects.You cannot resize the Form objectCan Name itCan change the Text property of the Pocket PC Form object

Page 5: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

ContainersWhy?

Help to keep things grouped together When used in conjunction with radio buttons, only

one button can be pushed at a time.Not all containers work in mobile devices.

Page 6: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Radio ButtonsHow did they get their name?Have properties just like any other object and therefore

Properties can be set at runtime or at design timeWe know a button has been selected by examining the checked propertyWe can also set a default value at design time.

Page 7: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Message BoxesGreat when doing data validation.When the user enters in a value that is not acceptable they can send a message box back out to the user.

Page 8: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Coding a Message BoxMsgBox(“User Name is Missing”, MsgBoxStyle.OKCancel, “User name is missing”)

Often concatenate strings when using message boxes

Page 9: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Review of IF…Then Structures

What are the types?What do we use to evaluation most structures?Can we compare strings as we do numbers?Simple IF….THEN construct

Page 10: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Logical OperatorsIn Addition to AND, OR and NOT there are

Page 11: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

The And Operator

AndAlso operator works identically but does not test minutes>12 if temperature<20 is false

The truth table for the And Operator

Expression 1 Expression 2 Expression 1 And Expression 2

True False FalseFalse True FalseFalse False FalseTrue True True

If temperature < 20 And minutes > 12 Then lblMessage.Text = “Temperature is in the danger zone."End If

Page 12: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

The Or Operator

OrElse operator works identically but does not test minutes>12 if temperature<20 is true

The truth table for the Or Operator

Expression 1 Expression 2 Expression 1 Or Expression 2

True False TrueFalse True TrueFalse False FalseTrue True True

If temperature < 20 Or temperature > 100 ThenlblMessage.Text = “Temperature is in the danger zone."

End If

Page 13: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

The Xor Operator

If total > 1000 Xor average > 120 ThenlblMessage.Text = “You may try again."

End If

The truth table for the Xor Operator

Expression 1 Expression 2 Expression 1 Or Expression 2

True False TrueFalse True TrueFalse False FalseTrue True False

Page 14: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

The Not OperatorThe truth table for the Not Operator

Expression 1 Not Expression 1

True FalseFalse True

If Not temperature > 100 ThenlblMessage.Text = "You are below the maximum temperature."

End If

Page 15: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Order of Precedence

Page 16: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Comparing Different DatatypesEvery 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

Page 17: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Relational Operators with Math Operators

Either or both relational operator operands may be mathematical expressionsMath operators are evaluated before relational operators

If x + y > a - b ThenlblMessage.Text = "It is true!"

End If

Page 18: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Relational Operators With Function Calls

Either or both relational operator operands may be function calls

If CInt(txtInput.Text) < 100 ThenlblMessage.Text = "It is true!"

End If

Page 19: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Checking Numerical Ranges

Checking for a value inside a range uses And

Checking for a value outside a range uses Or

If x >= 20 And x <= 40 ThenlblMessage.Text = “Value is in the acceptable range."

End If

If x < 20 Or x > 40 ThenlblMessage.Text = “Value is outside the acceptable range."

End If

Page 20: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

How Are Strings Compared?

Each character is encoded as a numerical value using the Unicode standardLetters are arranged in alphabetic order

The Unicode numeric code for A is less than the Unicode numeric code for B

Page 21: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

IsNumeric FunctionThis function accepts a string as an argument and returns True if the string contains a number

Dim strNumber as StringstrNumber = “576”If IsNumeric(strNumber) ‘returns truestrNumber = “123abc”If IsNumeric(strNumber) ‘returns false

Use IsNumeric function to determine if a given string contains numeric data

Page 22: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Determining the Length of a String

The Length method determines the length of a string, e.g.:If txtInput.Text.Length > 20 Then

lblMessage.Text = “Enter fewer than 20 characters."End If

Note: txtInput.Text.Length means to applythe Length Method to the value of the Text propertyof the Object txtInput

Page 23: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Trimming Spaces from Strings

There are three Methods that remove spaces from strings:

TrimStart – removes leading spaces TrimEnd – removes trailing spaces Trim – removes leading and trailing spaces

greeting = " Hello "lblMessage1.Text = greeting.TrimStart()

' Returns the value "Hello "

lblMessage1.Text = greeting.Trim()‘ Returns the value "Hello"

Page 24: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

The Substring MethodThe Substring method returns a portion of a string or a “string within a string” (a substring)Each character position is numbered sequentially with the 1st character referred to as position zeroStringExpression.Substring(Start)

returns the characters from the Start position to the end

StringExpression.Substring(Start, Length) returns the number of characters specified by Length

beginning with the Start position

Page 25: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Substring Method Examples

Dim firstName As StringDim fullName As String = "George Washington"firstName = fullName.Substring(0, 6)

' firstName assigned the value "George"' fullName is unchanged

lastName = fullName.Substring(7)‘ lastName assigned the value “Washington”‘ fullName unchanged

Position 0 Position 7

Page 26: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Search for a String Within a String

Use the IndexOf methodStringExpression.IndexOf(Searchstring)

Searches the entire string for SearchstringStringExpression.IndexOf(SearchString, Start)

Starts at the character position Start and searches for Searchstring from that point

StringExpr.IndexOf(SearchString, Start, Count)

Starts at the character position Start and searches Count characters for SearchString

Page 27: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

IndexOf Method Examples

IndexOf will return the starting position of the SearchString in the string being searchedPositions are numbered from 0 (for the first)

If SearchString is not found, a -1 is returned

Dim name As String = "Angelina Adams"Dim position As Integerposition = name.IndexOf("A", 1)

' position has the value 9

Position 0 Position 9

Page 28: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Boolean Variables as Flags

A flag is a Boolean variable that signals when some condition exists in the programSince a Boolean variable is either True or False, it can be used as the condition of an If

Since a Boolean variable already evaluates to True or False, an operator is not required

If blnQuotaMet ThenlblMessage.Text = "You have met your sales quota"

End If

Page 29: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

ToUpper MethodToUpper method can be applied to a string Results in a string with lowercase letters converted to uppercaseThe original string is not changed

littleWord = "Hello"bigWord = littleWord.ToUpper()

' littleWord retains the value "Hello"' bigWord is assigned the value "HELLO"

Page 30: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

ToLower MethodThe ToLower method performs a similar but opposite purposeCan be applied to a string Results in a string with the lowercase letters converted to uppercaseThe original string is not changed

bigTown = “New York"littleTown = bigTown.ToLower()

' bigTown retains the value “New York"' littleTown is assigned the value “new york"

Page 31: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

IF…THEN…ELSEElse is considered a different path through your code. If the condition is not true then the computer goes to the else statement.

Page 32: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

IF…THEN…ELSEIF…ELSE

Elseif acts like a nested if statementOften include ELSE to catch the condition if it were false.

Page 33: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Getting Back to our Radio Button

How do we know when the user has selected an optionThe checked property

Page 34: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Checking Radio Buttons in Code

If radCoffee.Checked = True ThenMessageBox.Show("You selected Coffee")

ElseIf radTea.Checked = True ThenMessageBox.Show("You selected Tea")

ElseIf radSoftDrink.Checked = True ThenMessageBox.Show("You selected a Soft Drink")

End If

Page 35: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Check BoxesUnlike radio buttons, can select many check boxes at one timeMay also be placed in a group box

Not limited to one selection within a group box Can select as many check boxes as you like within

the same group boxCheck boxes also have a boolean Checked property and a CheckChanged event

Page 36: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Checking Check Boxes in Code

If chkChoice4.Checked = True Then MessageBox.Show("You selected Choice 4")

End If

Page 37: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Select CaseFinally!This decision structure construct is a great substitution for if..then…elseif statements.

Page 38: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Select Case Example

Page 39: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Using Relational Operators

Page 40: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Using Ranges

Page 41: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Code SnippetsIncluded in the library with your other Visual Studio objectsCan really speed up the coding process

Page 42: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Validating DataDevelopers should anticipate that users will enter invalid dataDevelopers must write code that will prevent the invalid data from being used in the program to produce invalid outputIsNumeric ~ A function that returns a boolean value if the argument entered in to the function is indeed a number

Page 43: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Message Box Arguments

A message box is a dialog box with a user message in a pop-up windowThe following can be specified

Message - text to display within the box Caption - title for the top bar of the box Buttons - indicates which buttons to display Icon - indicates icon to display DefaultButton - indicates which button corresponds

to the Return Key All arguments but the Message are optional Use of an argument requires those before it

Page 44: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

MessageBox Buttons Argument

MessageBoxButtons.AbortRetryIgnoreDisplays Abort, Retry, and Ignore

buttonsMessageBoxButtons.OK

Displays only an OK buttonMessageBoxButtons.OKCancel

Displays OK and Cancel buttonsMessageBoxButtons.RetryCancel

Display Retry and Cancel buttonsMessageBoxButtons.YesNo

Displays Yes and No buttonsMessageBoxButtons.YesNoCancel

Displays Yes, No, and Cancel buttons

Page 45: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

MessageBox Icon Argument

The Icon argument specifies a particular type of icon to appear in the message box

There are 4 possible icons shown to the left

Note that some values show the same icon

Page 46: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Example Message Box

MessageBox.Show("Do you wish to continue?", _"Please Confirm", _MessageBoxButtons.YesNo, _MessageBoxIcon.Question)

Page 47: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Which Button Was Clicked

MessageBox returns a value indicating which button the user clicked:

DialogResult.Abort DialogResult.Cancel DialogResult.Ignore DialogResult.No DialogResult.OK DialogResult.Retry DialogResult.Yes

Page 48: I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for

Which Button Was Clicked Example

Dim result As Integerresult = MessageBox.Show("Do you wish to continue?", _

"Please Confirm", MessageBoxButtons.YesNo)

If result = DialogResult.Yes Then' Perform an action here

ElseIf result = DialogResult.No Then' Perform another action here

End If