1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual...

120
1 Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output

Transcript of 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual...

Page 1: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

1

Chapter 3 – Fundamentals of Programming in Visual Basic

• 3.1 Visual Basic Controls

• 3.2 Visual Basic Events

• 3.3 Numbers

• 3.4 Strings

• 3.5 Input and Output

Page 2: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

2

3.1 Visual Basic Controls• Invoking Visual Basic • Text Box Control• Button Control• Label Control• List Box Control• Name Property • Fonts / Auto Hide• Positioning and Aligning Controls

Page 3: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

IN-CLASS EXAMPLE

Make a small program or two

The following slides show screen shots of the compiler, I’ll keep them up for review.

Section 3.3 Numbers Next

3

Page 4: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

4

Visual Basic Start Page

Page 5: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

5

Start a New Project

Page 6: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

6

New Project Dialog Box

Page 7: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

7

Initial Visual Basic Screen

Page 8: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

8

Toolbox

Page 9: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

9

3 Ways to Place a Control from the Toolbox onto the Form Window

• Double-click

• Drag

• Click, Point, and Drag

Page 10: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

10

Four Controls at Design Time

Text box

To select a control, click on it. Sizing handles will appear when a control is selected.

Page 11: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

11

Text Box Control• Used for input and output• When used for output, ReadOnly

property is set to True

Sizing handles

Tasks button

Page 12: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

12

Properties Window

Categorized view Alphabetical view

Press F4 to display the

Properties window for the selected

control.

Page 13: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

13

Properties Window

PropertiesSettings

Selected control

Page 14: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

14

Some Often Used Properties

• Text

• Autosize

• Font.Name

• Font.Size

• ForeColor

• BackColor

• ReadOnly

Page 15: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

15

Setting Properties

• Click on property name in left column.

• Enter its setting into right column by typing or selecting from options displayed via a button or ellipses.

Page 16: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

16

Setting the ForeColor Property

1. Click on ForeColor.

2. Click on button at right of settings box.

3. Click on Custom tab to obtain display shown.

4. Click on a color.

Page 17: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

17

Font Property1. Click on

Font in left column.

2. Click on ellipsis at right of settings box to obtain display shown,

3. Make selections.

Page 18: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

18

Button Control• The caption on the button should indicate

the effect of clicking on the button.• Text property determines caption.

Page 19: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

19

Add an "access key"

Page 20: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

20

Label Control• Used to identify the contents of a text box.• Text property specifies caption.• By default, label automatically resizes to

accommodate caption on one line.• When the AutoSize property is set to False,

label can be resized manually. Used primarily to obtain a multi-rowed label.

Page 21: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

21

List Box Control

• Initially used to display several pieces of output.

• Later used to select from a list.

Page 22: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

22

The Name Property• How the programmer refers to a control in

code• Setting for Name property near top of

Properties window.• Name must begin with a letter, be less than

215 characters long, and may include numbers and letters.

• Use appropriate 3- or 4-character naming prefix

Page 23: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

23

Control Name Prefixes

Control Prefix Example

button btn btnCompute

label lbl lblAddress

text box txt txtAddress

list box lst lstOutput

Page 24: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

24

Renaming the Form

• Initial name is Form1

• The Solution Explorer window lists a file named Form1.vb.

• To rename the form, change the name of this file to newName.vb

• newName should begin with prefix frm.

Page 25: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

25

Fonts

• Proportional width fonts take up less space for "I" than for "W" – like Microsoft Sans Serif

• Fixed-width fonts take up the same amount of space for each character – like Courier New

• Fixed-width fonts are good for tables.

Page 26: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

26

Auto Hide

• Hides Toolbox when not in use• Vertical push pin icon indicates auto hide is

disabled.• Click the push pin to make it horizontal and

enable auto hide.

Push pin

Page 27: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

27

Positioning Controls

Proximity line

Page 28: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

28

Aligning Controls

Snap line

Page 29: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

29

Aligning Controls

Snap line

Page 30: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

30

3.2 Visual Basic Events

• An Event Procedure Walkthrough

• Properties and Event Procedures of the Form

• The Header of an Event Procedure

Page 31: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

31

Event

• An event is an action, such as the user clicking on a button

• Usually, nothing happens in a Visual Basic program until the user does something and generates an event.

• What happens is determined by statements.

Page 32: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

32

Sample Statements

• txtBox.ForeColor = Color.Red

• txtBox.Visible = True

• txtBox.Text = “Hello World”

General Form:

controlName.property = setting

Page 33: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

33

Sample Form

txtFirst

txtSecond

btnRed

Page 34: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

34

Focus

• When you click on a text box, a cursor appears in the text box, and you can type into the text box.

• Such a text box is said to have the focus.

• If you click on another text box, the first text box loses the focus and the second text box receives the focus.

Page 35: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

35

Examples of Events

• btnShow.Click

• txtBox.TextChanged

• txtBox.Leave

General Form:

controlName.event

Page 36: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

36

The three steps in creating a Visual Basic program:

1. Create the interface; that is, generate, position, and size the objects.

2. Set properties; that is, configure the appearance of the objects.

3. Write the code that executes when events occur.

Page 37: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

37

Code Window

Method Name box

Class Name box

Page tab

Page 38: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

38

Structure of an Event Procedure

Private Sub objectName_event(...)

Handles objectName.event

statements

End Sub

(...) is filled automatically with (ByVal sender As System.Object, ByVal e As System.EventArgs)

Header

Page 39: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

39

Code Window

Method Name box

Class Name box

Page tab

Page 40: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

40

Create an Outline for an Event Procedure; i.e. header and End Sub

1. Double-click on a control

or

2. Use the Class Name and Method

Name boxes.

(We primarily use the first

method.)

Page 41: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

41

Sample Form

txtFirst

txtSecond

btnRed

Double Click on txtFirst

Page 42: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

42

Code for WalkthroughPublic Class frmDemo

Private Sub txtFirst_TextChanged(...)

Handles txtFirst.TextChanged

txtFirst.ForeColor = Color.Blue

End Sub

End Class

Page 43: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

43

IntelliSense

Automatically pops up to give the programmer help.

Page 44: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

44

Code Window

Click tab to return to Form Designer

Page 45: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

45

Sample Form

txtFirst

txtSecond

btnRed

Double-click on btnRed

Page 46: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

46

Code for WalkthroughPublic Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub

Private Sub btnRed_Click(...) Handles btnRed.Click

End SubEnd Class

Page 47: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

47

Code for WalkthroughPublic Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub

Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End SubEnd Class

Page 48: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

48

Event Procedure txtFirst.Leave

• Select txtFirst from Class Name box drop-down list.

• Select Leave from Method Name box drop-down list.

Page 49: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

49

Code for WalkthroughPrivate Sub txtFirst_Leave(...) Handles txtFirst.Leave End Sub

Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.BlueEnd Sub

Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.RedEnd Sub

Page 50: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

50

Code for WalkthroughPrivate Sub txtFirst_Leave(...) Handles txtFirst.Leave txtFirst.ForeColor = Color.BlackEnd Sub

Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.BlueEnd Sub

Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.RedEnd Sub

Page 51: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

51

Header of Event ProcedurePrivate Sub btnRed_Click(…) Handles btnRed.Click

Identifies eventName, can be changed.

Private Sub Button_Press(…) Handles btnRed.Click

Page 52: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

52

Handling Multiple Events

Private Sub Button_Click(...) Handles btnRed.Click, txtSecond.Leave txtFirst.ForeColor = Color.RedEnd Sub

Event procedure can be invoked by two events.

Page 53: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

53

Altering Properties of the Form

• The following won't work: frmDemo.Text = "Demonstration"

• The form is referred to by the keyword Me.

Me.Text = "Demonstration"

Page 54: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

54

3.3 Numbers

• How to handle numbers in VB• Arithmetic Operations • Variables • Incrementing the Value of a Variable • Built-In Functions:

• Math.Sqrt• Int• Math.Round

Page 55: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

55

Numbers continued

• The Integer Data Type

• Multiple Declarations

• Parentheses

• Three Types of Errors

Page 56: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

56

Arithmetic Operations

• Numbers are called numeric literals

• Five arithmetic operations in Visual Basic• + addition• - subtraction• * multiplication• / division• ^ exponentiation

Page 57: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

57

Numeric Expressions

• 2 + 3

• 3 * (4 + 5)

• 2 ^ 3

Page 58: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

58

Displaying Numbers

Let n be a number or a numeric expression.

The statement

lstBox.Items.Add(n)

displays the value of n in the list box.

Page 59: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

Numbers Example

• List Box Example

Page 60: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

60

Example 1: Form

Page 61: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

61

Example 1: Code and OutputPrivate Sub btnCompute_Click (...) Handles btnCompute.Click lstResults.Items.Add(5) lstResults.Items.Add(2 * 3) lstResults.Items.Add((2 ^ 3) – 1)End Sub

Output 5in list 6box 7

Page 62: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

62

Example 1: Code using WithPrivate Sub btnCompute_Click (...)

Handles btnCompute.Click

With lstResults.Items

.Add(5)

.Add(2 * 3)

.Add((2 ^ 3) – 1)

End With

End Sub

Page 63: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

63

Numeric Variable

A numeric variable is a name to which a number can be assigned.

Examples:

speeddistance

interestRatebalance

Page 64: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

64

Variables

• Declaration:Dim speed As Double

Variable name Data type

• Assignment:speed = 50

Page 65: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

65

Initialization

• Numeric variables are automatically initialized to 0:

Dim varName As Double

• To specify a nonzero initial value

Dim varName As Double = 50

Page 66: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

66

Numeric Expressions

Numeric variables can be used in numeric

expressions.

Dim balance As Double = 1000

lstBox.Items.Add(1.05 * balance)

Output: 1050

Page 67: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

67

Assignment Statement

Dim numVar1 As Double = 5

Dim numVar2 As Double = 4

numVar1 = 3 * numVar2

lstBox.Items.Add(numVar1)

Output: 12

Page 68: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

68

Incrementing

• To add 1 to the numeric variable varvar = var + 1

• Or as a shortcutvar += 1

• Or as a generalizationvar += numeric expression

Page 69: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

69

Built-in Functions

• Functions return a value

Math.Sqrt(9) returns 3

Int(9.7) returns 9

Math.Round(2.7) is 3

Page 70: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

70

Integer Data Type• Variables of type Double can be assigned

both whole numbers and numbers with decimals.

• The statement Dim varName As Integer declares a numeric variable that can only be assigned whole number values

between about -2 billion and 2 billion.

Page 71: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

71

Multiple Declarations

Dim a, b As Double

Two other types of multiple-declaration statements are

Dim a As Double, b As Integer

Dim c As Double = 2, b As

Integer = 5

Page 72: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

72

Parentheses

• Parentheses should be used liberally in numeric expressions.

• In the absence of parentheses, the operations are carried out in the following order: ^, * and /, + and -.

• Follows standard order of operations

Page 73: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

73

Three Types of Errors

• Syntax error

• Run-time error

• Logic error

Page 74: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

74

Some Types of Syntax Errors

• Misspellings

lstBox.Itms.Add(3)• Omissions

lstBox.Items.Add(2 + )• Incorrect punctuation

Dim m; n As Integer

Page 75: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

75

A Type of Run-time Error

• Overflow error

Dim numVar As Integer = 1000000

numVar = numVar * numVar

Page 76: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

76

A Logical Error

Dim average As Double

Dim m As Double = 5

Dim n As Double = 10

average = m + n / 2

Value of average will be 10. Should be 7.5.

Page 77: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

77

3.4 Strings• Variables and Strings• Using Text Boxes for Input and Output• Concatenation

• String Properties and Methods:

•Length •ToUpper

•Trim •ToLower

•IndexOf •Substring

Page 78: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

78

Strings continued

• The Empty String

• Initial Value of a String

• Option Strict

• Internal Documentation

• Line-Continuation Character

Page 79: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

79

String Literal

A string literal is a sequence of

characters surrounded by quotation marks.

Examples:"hello"

"123-45-6789"

"#ab cde?"

Page 80: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

80

String Variable

A string variable is a name to which a

string value can be assigned.

Examples:

country

ssn

word

firstName

Page 81: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

81

String Variable

• Declaration:Dim firstName As String

Variable nameData type

• Assignment:firstName = "Fred"

Page 82: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

82

String Variable

You can declare a string variable and

assign it a value at the same time.

Dim firstName As String = "Fred"

Page 83: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

83

Add Method

Let str be a string literal or variable. Then,

lstBox.Items.Add(str)

displays the value of str in the list box.

Page 84: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

84

String Variable

You can assign the value of one string

variable to another.

Dim strVar1 As String = "Hello"

Dim strVar2 As String = "Goodbye"

strVar2 = strVar1

lstOutput.Items.Add(strVar2)

Output: Hello

Page 85: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

85

Variables and StringsPrivate Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim today As String today = "Monday" lstOutput.Items.Add("hello") lstOutput.Items.Add(today)End Sub

Output: hello Monday

Page 86: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

86

Using Text Boxes for Input and Output

• The contents of a text box is always a string

• Input example

strVar = txtBox.Text

• Output example

txtBox.Text = strVar

Page 87: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

87

Data Conversion

• Because the contents of a text box is always a string, sometimes you must convert the input or output.

dblVar = CDbl(txtBox.Text)

txtBox.Text = CStr(numVar)Converts a String to a Double

Converts a number to a string

Page 88: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

88

Concatenation

Combining two strings to make a new string

quote1 = "We'll always "quote2 = "have Paris."quote = quote1 & quote2txtOutput.Text = quote & " - Humphrey Bogart"

DisplaysWe'll always have Paris. - Humphrey Bogart

Page 89: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

89

Appending

• To append str to the string variable varvar = var & str

• Or as a shortcutvar &= str

Page 90: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

90

Appending ExampleDim var As String = "Good"

var &= "bye"

txtBox.Text = var

OUTPUT: Goodbye

Page 91: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

91

String Properties and Methods "Visual".Length is 6.

"Visual".ToUpper is VISUAL.

"123 Hike".Length is 8.

"123 Hike".ToLower is 123 hike.

"a" & " bcd ".Trim & "efg" is abcdefg.

Note the spaces before and after bcd

Page 92: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

92

Positions in a String Positions of characters in a string are numbered

0, 1, 2, ….

Consider the string “Visual Basic”.

Position 0: V

Position 1: i

Position 7: B

Substring “al” begins at position 4

Page 93: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

93

Substring Method Let str be a string.

str.Substring(m, n) is the substring of length

n, beginning at position m in str.

“Visual Basic”.Substring(2, 3) is “sua”

“Visual Basic”.Substring(0, 1) is “V”

Page 94: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

94

IndexOf Method Let str1 and str2 be strings.

str1.IndexOf(str2)

is the position of the first occurrence of str2 in str1.

(Note: Has value -1 if str2 is not a substring of str1.)

"Visual Basic".IndexOf("is") is 1.

"Visual Basic".IndexOf("si") is 9.

"Visual Basic".IndexOf("ab") is -1.

Page 95: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

95

The Empty String • The string "", which contains no characters, is

called the empty string or the zero-length string.• The statement lstBox.Items.Add("") skips a

line in the list box. • The contents of a text box can be cleared with

either the statement txtBox.Clear()• or the statement txtBox.Text = ""

Page 96: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

96

Initial Value of a String

• By default the initial value is Nothing

• Strings can be given a different initial value as follows:

Dim today As String = "Monday"

Page 97: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

97

Option Strict

• Visual Basic allows numeric variables to be assigned strings and vice versa, a poor programming practice.

• To turn this feature off, put the following statement at the very top of the code window

Option Strict On

Page 98: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

98

Option Strict On for All Programs

• Select Options from the Tools menu

• In left pane, expand Projects and Solution

• Select VB Defaults

• Set Option Strict to On

Page 99: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

99

With Option Strict OnDim dblVar As Double, intVar As Integer

Dim strVar As String

Not Valid: Replace with:intVar = dblVar intVar = CInt(dblVar)

dblVar = strVar dblVar = CDbl(strVar)

strVar = intVar strVar = CStr(intVar)

Page 100: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

100

CommentsPrivate Sub btnCompute_Click (...)

Handles btnCompute.Click

'Calculate the balance in an account

Dim rate As Double 'Annual rate of interest

Dim curBalance As Double 'Current balance

Page 101: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

101

Internal Documentation

1. Other people can easily understand the program.

2. You can understand the program when you read it later.

3. Long programs are easier to read because the purposes of individual pieces can be determined at a glance.

Page 102: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

102

Automatic Colorization

Comments – green

String literals – maroon

Keywords – blue

Note: Keywords are words such as Sub,

Handles, Private, With, and End that have

special meaning in Visual Basic. They

cannot be used as variable names.

Page 103: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

103

Line-Continuation Character• A long line of code can be continued on another

line by using an underscore (_) preceded by a space

msg = "I'm going to make " & _

"him an offer he can't refuse.“

This is for if your line of code is to long for one line.

Page 104: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

104

3.5 Input and Output

• Formatting Output with Format Functions

• Formatting Output with Zones

• Reading Data from Files

• Getting Input from an Input Dialog Box

• Using a Message Dialog Box for Output

• Using a Masked Text Box for Input

Page 105: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

105

Formatting Output with Format Functions

Function String Value

FormatNumber(12345.628, 1) 12,345.6

FormatCurrency(12345.628, 2) $12,345.63

FormatPercent(0.183, 0) 18%

Page 106: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

106

Formatting Output with Zones

• Use a fixed-width font such as Courier New

• Divide the characters into zones with a format string.

Dim fmtStr As String = "{0, 15}{1, 10}{2, 8}"

lstOutput.Items.Add(String.Format(fmtStr, _

data0, data1, data2))

Page 107: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

107

Formatting Output with ZonesDim fmtStr As String = "{0, -15}{1, 10}{2, 8}"

lstOutput.Items.Add(String.Format(fmtStr, _

data0, data1, data2))

Here, 15 was preceded by a minus sign. This

produces left justification in 0th zone. There will

be right justification in the other two zones.

Page 108: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

108

Zone Formatting Symbols

Symbols: N, C, and P Effect on zone

:Nr FormatNumber(data, r)

:Cr FormatCurrency(data, r)

:Pr FormatPercent(data, r)

Dim fmtStr As String = "{0,15:N1}{1,10:C2}{2,8:P0}"

Page 109: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

109

Reading Data from Files

• Data can be stored in files and accessed with a StreamReader object.

• We assume that the files are text files (that is, have extension .TXT) and have one piece of data per line.

Page 110: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

110

Sample File: PAYROLL.TXT

Mike Jones

7.35

35

John Smith

6.75

33

Name

Hourly wage

Number of hours worked

Page 111: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

111

Steps to Use StreamReaderExecute a statement of the form

Dim readerVar As IO.StreamReader = _ IO.File.OpenText(filespec)

or the pair of statements

Dim readerVar As IO.StreamReader readerVar = IO.File.OpenText(filespec)

Page 112: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

112

Steps to Use StreamReaderRead items of data in order, one at a time,from the file with the ReadLine method.

strVar = readerVar.ReadLine

After the desired items have been read fromthe file, terminate the communications link

readerVar.Close()

Page 113: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

113

Example using StreamReaderDim name As StringDim wage, hours As DoubleDim sr As IO.StreamReader = _ IO.File.OpenText("PAYROLL.TXT")name = sr.ReadLinewage = CDbl(sr.ReadLine)hours = CDbl(sr.ReadLine)lstBox.Items.Add(name & ": " & wage * hours)

OUTPUT: Mike Jones: 257.25

Page 114: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

114

Comment on Example

Consider

lstBox.Items.Add(name & ": " & wage * hours)

The ampersand automatically converted

wage * hours into a string before concatenating.

We didn’t have to convert wage * hours with CStr.

Page 115: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

115

Getting Input from an Input Dialog Box

stringVar = InputBox(prompt, title)

fileName = InputBox("Enter the name " _

& "of the file containing the " & _

"information.", "Name of File")

Title

Prompt

Page 116: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

116

Using a Message Dialog Box for OutputMsgBox(prompt, 0, title)

MsgBox("Nice try, but no cigar.", 0, _

"Consolation")

Title

Prompt

Page 117: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

117

Masked Text Box

Similar to an ordinary text box, but has a Mask property that restricts what can be typed into the masked text box.

Page 118: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

118

Input Mask Dialog Box

Page 119: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

119

Mask

A Mask setting is a sequence of characters, with 0, L, and & having special meanings.

0 Placeholder for a digit.

L Placeholder for a letter.

& Placeholder for a character or space.

Page 120: 1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.

120

Sample Masks

State abbreviation: LL

Phone number: 000-0000

Social Security Number: 000-00-0000

License plate: &&&&&&