Top Banner
InputBox
56

InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Jan 29, 2016

Download

Documents

Paulina Roberts
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

InputBox

Page 2: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Format of the InputBox Function

• Prompt - message to the user

• Title - text for the box's title bar

• Default - default text for user's input

• Xpos - X coordinate for the box's position

• Ypos - Y coordinate for the box's position

• Title and beyond are optional arguments

InputBox(Prompt [,Title] [,Default] [,Xpos] [,Ypos])

Page 3: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Sample InputBox Usage

• userInput = InputBox("Enter the distance.", "Provide a Value", "150")

Page 4: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Xpos, Ypos, and Twips

• Xpos specifies the distance from the left of the screen to the left side of the box

• Ypos, from the top of the screen to the top of the box

• Both are specified in twips

• One twip is 1/440 inch

Page 5: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The Decision Structure

The Decision Structure Allows a Program to Have More Than One

Path of Execution

Page 6: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Order of Statement Execution

• Thus far, all of our code statements have been executed sequentially

• To write meaningful programs our code needs to be able to• Execute code conditionally (this chapter)• Repeat sections of code (next chapter)

Page 7: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The Decision Structure

• Flowchart of atypical decisionstructure

• Evaluate thecondition

• Execute, or notsome code

Condition

ConditionalCode

True

False

Page 8: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The If…Then Statement

The If…Then Statement Causes Other Statements to Execute Only

Under a Certain Condition

Page 9: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

If…Then Statement Syntax

• New keywords used above:• If• Then• End

If condition Thenstatement(more statements as needed)

End If

Page 10: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Relational Operators

• Usually the condition is formed with a relational operator• > Greater than• < Less than• = Equal to• <> Not equal to• >= Greater than or equal to• <= Less than or equal to

Page 11: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Binary Operators

• Operators are classified as to how many operands each takes

• Relational operators are binary operators --each has two operands, e.g.• length > width• size <= 10

• Relational operators yield a True or False result

Page 12: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

If…Then Examples

If sales > 50000 ThengetsBonus = True

End If

If sales > 50000 ThengetsBonus = TruecommissionRate = 0.12daysOff = daysOff + 1

End If

Page 13: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

If…Then Rules

• The 'If' and the 'Then' must be on the same line

• Only a remark may follow the 'Then'

• The 'End If' must be on a separate line

• Only a remark may follow the 'End If'

Page 14: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

If…Then Conventions

• The code between the 'If…Then' and the 'End If' is indented

• Visual Basic .NET does not require this• It is a convention among programmers to

aid in the readability of programs• By default, the Visual Basic .NET editor

will automatically do this indentation as you enter your program

Page 15: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Relational Operators with Math Operators

• Either or both of the relational operator operands may themselves be expressions

• Math operators are done before the relational operators

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

End If

Page 16: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Relational Operators with Function Calls

• Either or both of the relational operator operands may themselves be function calls

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

End If

Page 17: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Boolean Variables as Flags

• A flag is a Boolean variable that signals when some condition exists in the program

• They can be used as the condition

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

End If

Page 18: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

What an 'If…Then' Really Tests For

• Visual Basic .NET first evaluates the conditional expression

• If the result is 0, the condition is regarded as being False

• If the result is anything else, the condition is regarded as being True

Page 19: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The If…Then…Else Statement

The If...Then...Else Statement Executes One Group of Statements If the Condition Is

True and Another Group of Statements If the Condition Is False

Page 20: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

'If…Then' vs. 'If…Then…Else'

• The 'If…Then' construct will execute or not a group of statements (do something or do nothing)

• The 'If…Then…Else' construct will execute one group of statements or another group (do this or do that)

Page 21: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The 'If…Then…Else' Structure

Condition

ExecuteIf True

TrueFalse

ExecuteIf False

Page 22: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

'If…Then…Else' Example

If temperature < 40 ThenlblMesage.Text = “A little cold, isn’t it?”

ElselblMesage.Text = “Nice weather we’re having!”

End If

Page 23: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

4.5The If…Then…ElseIf Statement

The If...Then…Elseif Statement Is Like a Chain of If...Then...Else Statements

They Perform Their Tests, One After the Other, Until One of Them Is Found to Be True

Page 24: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Two Mutually Exclusive Choices('If…Then…Else')

• The 'If…Then…Else' statement has two choices

• The conditional statement will either be True or False

• Hence, exactly one of the two choices will be selected

• They are mutually exclusive

Page 25: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

More Than TwoMutually Exclusive Choices

• The 'If…Then…ElseIf' statement provides a

series of mutually exclusive choices

• In pseudo code:If it is very cold Then

Wear a coatElseif it is chilly

Wear a light jacketElseif it is windy

Wear a windbreakerElesif it is hot

Wear no jacket

Page 26: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

In Visual Basic .NET Syntax

If condition1 ThenStatement(s)1

Elseif condition2 ThenStatements(s)2

Elseif condition3 ThenStatements3

…End If

Page 27: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

In Flowchart Form

C1

C2

C3

Statement(s)1

True

Statement(s)2

True

Statement(s)3

True

False

False

False

Page 28: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Example of Usage

If average < 60 ThenlblGrade.Text = "F"

ElseIf average < 70 ThenlblGrade.Text = "D"

ElseIf average < 80 ThenlblGrade.Text = "C"

ElseIf average < 90 ThenlblGrade.Text = "B"

ElseIf average <= 100 ThenlblGrade.Text = "A"

End If

Page 29: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Contrast the Preceding CodeTo This Code Without the Elses

If average < 60 ThenlblGrade.Text = "F"

End IfIf average < 70 Then

lblGrade.Text = "D"End IfIf average < 80 Then

lblGrade.Text = "C"End IfIf average < 90 Then

lblGrade.Text = "B"End IfIf average <= 100 Then

lblGrade.Text = "A"End If

Page 30: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The (Optional) Trailing Else

• A sequence of ElseIfs may end with a plain else (called a trailing Else)

• If none of the conditions were True, the statement(s) after this Else will be executed

• Continuing with the preceding example on the next slide:

Page 31: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Use of a Trailing Else

If average < 60 ThenlblGrade.Text = "F"

ElseIf average < 70 ThenlblGrade.Text = "D"

ElseIf average < 80 ThenlblGrade.Text = "C"

ElseIf average < 90 ThenlblGrade.Text = "B"

ElseIf average <= 100 ThenlblGrade.Text = "A"

ElselblGrade.Text = "Invalid"

End If

Page 32: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Nested If Statements

A Nested If Statement Is an If Statement in the Conditionally Executed Code of Another If

Statement

Page 33: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

'If' Statements Within 'If' Statements

• Inside of any of the styles of 'If' statements what we have seen, the statement(s) portion can be any combination of statements

• This includes other 'If' statements

• Hence more complex decision structures can be created (by nesting 'Ifs' within 'Ifs')

Page 34: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Consider This Code

If Val(txtSalary.Text) > 30000 ThenIf Val(txtYearsOnJob.Text) > 2 Then

lblMessage.Text = "The applicant qualifies."Else

lblMessage.Text = "The applicant does not qualify."End If

ElseIf Val(txtYearsOnJob.Text) > 5 Then

lblMessage.Text = "The applicant qualifies."Else

lblMessage.Text = "The applicant does not qualify."End If

End If

Note how the convention of indentationsemphasizes the structure of nested Ifs.

Page 35: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Flowchart Version

Val(txtSalary.Text) > 30000

Val(txtSalary.Text) > 30000

Val(txtSalary.Text) > 30000

lblMessage.Text = "The applicant

qualifies."

lblMessage.Text = "The applicantdoes

not qualify."

lblMessage.Text = "The applicant

qualifies."

lblMessage.Text = "The applicantdoes

not qualify."

False

False False

True

TrueTrue

Page 36: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Logical Operators

Logical Operators Connect Two or More Relational Expressions Into One, or Reverse the Logic of an

Expression

Page 37: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Visual Basic .NET Logical Operators

Operator EffectAnd Both operands must be true for the overall

expression to be true, otherwise it is falseOr One or both operands must be true for the overall

expression to be true, otherwise it is falseNot Reverses the logical value of an expression

Page 38: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The 'And' Operator

Truth Table for 'And' OperatorExpression 1 Expression 2 Expression 1 And Expression 2

True False FalseFalse True FalseFalse False FalseTrue True True

If temperature < 20 And minutes > 12 ThenlblMessage.Text = "The temperature is in the danger zone."

End If

Page 39: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The 'Or' Operator

Truth Table for 'Or' OperatorExpression 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 = "The temperature is in the danger zone."

End If

Page 40: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The 'Not' Operator

Truth Table for 'Not' OperatorExpression 1 Not Expression 1

True FalseFalse True

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

End If

Page 41: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Checking Numerical Ranges

• Checking for a value inside of a range:

• Checking for a value outside of a range:

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

End If

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

End If

Page 42: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The Message Box

Sometimes You Need a Convenient Way to Display a Message to the User

This Section Introduces the Messagebox.Show Method, Which Allows You to Display a

Message in a Dialog Box

Page 43: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Message Box Arguments

• 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

• Arguments are optional bottom to top

Page 44: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Buttons Argument

Value - DescriptionMessageBoxButtons.AbortRetryIgnore -

Displays Abort, Retry, and Ignore buttons.MessageBoxButtons.OK - Displays only an OK button.MessageBoxButtons.OKCancel - Displays OK and Cancel buttons.MessageBoxButtons.RetryCancel -

Display Retry and Cancel buttons.MessageBoxButtons.YesNo- -Displays Yes and No buttons.MessageBoxButtons.YesNoCancel -

Displays Yes, No, and Cancel buttons.

Page 45: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Example Message Box

MessageBox.Show("Do you wish to continue?", _"Please Confirm", _

MessageBoxButtons.YesNo)

Page 46: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

The Select Case Statement

In a Select Case Statement, One of Several Possible Actions Is Taken, Depending on the

Value of an Expression

Page 47: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Select Case Statement Example, ISelect Case Val(txtInput.Text)

Case 1MessageBox.Show("Day 1 is Monday.")

Case 2MessageBox.Show("Day 2 is Tuesday.")

Case 3MessageBox.Show("Day 3 is Wednesday.")

Case 4MessageBox.Show("Day 4 is Thursday.")

Case 5MessageBox.Show("Day 5 is Friday.")

Case 6MessageBox.Show("Day 6 is Saturday.")

Case 7MessageBox.Show("Day 7 is Sunday.")

Case ElseMessageBox.Show("That value is invalid.")

End Select

Page 48: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Select Case Statement Example, II

Select Case animalCase "Dogs", "Cats"

MessageBox.Show ("House Pets")Case "Cows", "Pigs", "Goats"

MessageBox.Show ("Farm Animals")Case "Lions", "Tigers", "Bears"

MessageBox.Show ("Oh My!")End Select

Page 49: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Input Validation

Input Validation Is the Process of Inspecting Input Values and Determining

Whether They Are Valid

Page 50: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Validation Example

' Validate the input to ensure that' no negative numbers were entered.

If sales < 0 Or advance < 0 ThenMessageBox.Show("Please enter positive numbers for " & _" sales and/or advance pay.")

EndIf

Page 51: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

4.12Radio Buttons and

Check Boxes

Radio Buttons Appear in Groups of Two or More, and Allow the User to Select One of Several Possible Options

Check Boxes, Which May Appear Alone or in Groups, Allow the User to Make Yes/no or On/off Selections

Page 52: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Checking Radio Buttons in Code

If radChoice1.Checked = True ThenMessageBox.Show("You selected Choice 1")

ElseIf radChoice2.Checked = True ThenMessageBox.Show("You selected Choice 2")

ElseIf radChoice3.Checked = True ThenMessageBox.Show("You selected Choice 3")

End If

Page 53: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Checking Check Boxes in Code

If chkChoice4.Checked = True Thenmessage = message & " and Choice 4"

End If

Page 54: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Class-level Variables

Class-level Variables Are Not Local to Any Procedure

In a Form File They Are Declared Outside of Any Procedure, and May Be Accessed by Statements in

Any Procedure in the Same Form

Page 55: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Advantages of Class-level Variables

• The scope of class-level variables include all of the procedures below the declaration in the file

• Hence, with the use of class-level variables, communication of information between modules is very easy

Page 56: InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.

Disadvantages of Class-level Variables

• Class-level variables should be used sparingly - only when really needed

• Why?

• In larger programs, the uses of these variables will be more difficult to keep track of and hence tend to introduce bugs into the procedures