Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10...

122
Chapter 3 - VB 2005 by Schneider 1 Introducing Visual Basic… September 8, 2006

Transcript of Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10...

Page 1: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 1

Introducing Visual Basic…

September 8, 2006

Page 2: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 2

Today…

• Chapter 3 provides a great walkthroughintro to VB

• We’re going to take a detailed look at thistoday (and pick it up next week)…

• We’ll keep the pace even, but feel free tooffer feedback to the prof……we can always adjust as necessary

depending on the needs of the class!

Page 3: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 3

Next week…

• Kim’s going to be back• She’ll keep going with today’s material

(and a quick recap)• Be prepared for your first assignment

later next week…• Also, Kim will get the website up this

weekend!• She promises… ;)

Page 4: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 4

Chapter 3 – Fundamentals ofProgramming 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 5: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 5

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 6: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 6

Visual Basic Start Page

Page 7: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 7

Start a New Project

Page 8: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 8

New Project Dialog Box

Page 9: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 9

Initial Visual Basic Screen

Page 10: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 10

Toolbox

Page 11: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 11

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

• Double-click

• Drag

• Click, Point, and Drag

Page 12: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 12

Four Controls at Design Time

Text box

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

Page 13: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 13

Text Box Control

• Used for input and output

• When used for output, ReadOnlyproperty is set to True

Sizing handles

Tasks button

Page 14: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 14

Properties Window

Categorized view Alphabetical view

Press F4 todisplay theProperties

window for theselected control.

Page 15: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 15

Properties Window

PropertiesSettings

Selectedcontrol

Page 16: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 16

Some Often Used Properties

• Text

• Autosize

• Font.Name

• Font.Size

• ForeColor

• BackColor

• ReadOnly

Page 17: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 17

Setting Properties

• Click on property name in left column.

• Enter its setting into right column bytyping or selecting from optionsdisplayed via a button or ellipses (…).

Page 18: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 18

Setting the ForeColor Property

1. Click on ForeColor.

2. Click on button at rightof settings box.

3. Click on Custom tab toobtain display shown.

4. Click on a colour.

Page 19: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 19

Font Property1. Click on Font in

left column.

2. Click on ellipsisat right ofsettings box toobtain displayshown,

3. Makeselections.

Page 20: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 20

Button Control

• The caption on the button should indicatethe effect of clicking on the button.

• Text property determines caption.

Page 21: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 21

Add an "access key"

Page 22: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 22

Label Control

• Used to identify the contents of a text box.

• Text property specifies caption.

• By default, label automatically resizes toaccommodate caption on one line.

• When the AutoSize property is set to False,label can be resized manually. Used primarilyto obtain a multi-rowed label.

Page 23: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 23

List Box Control

• Initially used to display several pieces ofoutput.

• Later used to select from a list.

Page 24: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 24

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 numbersand letters.

• Use appropriate 3- or 4-character namingprefix

Page 25: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 25

Control Name Prefixes

lstOutputlstlist box

txtAddresstxttext box

lblAddresslbllabel

btnComputebtnbutton

ExamplePrefixControl

Page 26: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 26

Renaming the Form

• Initial name is Form1

• The Solution Explorer window lists a filenamed Form1.vb.

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

• newName should begin with prefix frm.

Page 27: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 27

Fonts

• Proportional width fonts take up lessspace for "I" than for "W" – like MicrosoftSans Serif

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

• Fixed-width fonts are good for tables.

Page 28: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 28

Auto Hide

• Hides Toolbox when not in use

• Vertical push pin icon indicates auto hide isdisabled.

• Click the push pin to make it horizontal andenable auto hide.

Push pin

Page 29: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 29

Positioning Controls

Proximityline

Page 30: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 30

Aligning Controls

Snap line

Page 31: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 31

Aligning Controls

Snap line

Page 32: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 32

3.2 Visual Basic Events

• An Event Procedure Walkthrough

• Properties and Event Procedures of theForm

• The Header of an Event Procedure

Page 33: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 33

Event

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

• Usually, nothing happens in a VisualBasic program until the user doessomething and generates an event.

• What happens is determined bystatements.

Page 34: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 34

Sample Statements

• txtBox.ForeColor = Color.Red

• txtBox.Visible = True

• txtBox.Text = “Hello World”

General Form:

controlName.property = setting

Page 35: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 35

Sample Form

txtFirst

txtSecond

btnRed

Page 36: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 36

Focus

• When you click on a text box, a cursorappears in the text box, and you can typeinto the text box.

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

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

Page 37: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 37

Examples of Events

• btnShow.Click

• txtBox.TextChanged

• txtBox.Leave

General Form:

controlName.event

Page 38: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 38

The three steps in creating aVisual Basic program:

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

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

3. Write the code that executes whenevents occur.

Page 39: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 39

Code Window

MethodNamebox

ClassNamebox

Page tab

Page 40: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 40

Structure of an Event Procedure

Private Sub objectName_event(...) Handles objectName.event statements End Sub

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

Header

Page 41: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 41

Code Window

MethodNamebox

ClassNamebox

Page tab

Page 42: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 42

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

1. Double-click on a control or2. Use the Class Name and Method Name boxes.

(We primarily use the first method.)

Page 43: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 43

Sample Form

txtFirst

txtSecond

btnRed

Double Click on txtFirst

Page 44: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 44

Code for WalkthroughPublic Class frmDemo Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged

End SubEnd Class

Page 45: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 45

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

Page 46: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 46

IntelliSense

Automatically pops up to give the programmer help.

Page 47: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 47

Code Window

Click tab to return to Form Designer

Page 48: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 48

Sample Form

txtFirst

txtSecond

btnRed

Double-click on btnRed

Page 49: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 49

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 50: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 50

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 51: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 51

Event Procedure txtFirst.Leave

• Select txtFirst from Class Name boxdrop-down list.

• Select Leave from Method Name boxdrop-down list.

Page 52: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 52

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 53: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 53

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 54: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 54

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

Identifies eventName, canbe changed.

Private Sub Button_Press(…) Handles btnRed.Click

Page 55: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 55

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 56: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 56

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 57: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 57

3.3 Numbers

• Arithmetic Operations

• Variables

• Incrementing the Value of a Variable

• Built-In Functions:• Math.Sqrt

• Int

• Math.Round

Page 58: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 58

Numbers continued

• The Integer Data Type

• Multiple Declarations

• Parentheses

• Three Types of Errors

Page 59: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 59

Arithmetic Operations

• Numbers are called numeric literals

• Five arithmetic operations in Visual Basic• + addition

• - subtraction

• * multiplication

• / division

• ^ exponentiation

Page 60: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 60

Numeric Expressions

• 2 + 3

• 3 * (4 + 5)

• 2 ^ 3

Page 61: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 61

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 62: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 62

Example 1: Form

Page 63: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 63

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 64: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 64

Example 1: Code using WithPrivate Sub btnCompute_Click (...) Handles btnCompute.Click With lstResults.Items .Add(5) .Add(2 * 3) .Add((2 ^ 3) – 1) End WithEnd Sub

Page 65: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 65

Numeric Variable

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

Examples:

speeddistance

interestRatebalance

Page 66: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 66

Variables

• Declaration:Dim speed As Double

Variable name Data type

• Assignment:speed = 50

Page 67: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 67

Initialization

• Numeric variables are automaticallyinitialized to 0:Dim varName As Double

• To specify a nonzero initial valueDim varName As Double = 50

Page 68: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 68

Numeric Expressions

Numeric variables can be used in numeric

expressions.

Dim balance As Double = 1000lstBox.Items.Add(1.05 * balance)

Output: 1050

Page 69: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 69

Assignment Statement

Dim numVar1 As Double = 5Dim numVar2 As Double = 4numVar1 = 3 * numVar2lstBox.Items.Add(numVar1)

Output: 12

Page 70: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 70

Incrementing

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

• Or as a shortcutvar += 1

• Or as a generalizationvar += numeric expression

Page 71: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 71

Built-in Functions

• Functions return a value

Math.Sqrt(9) returns 3

Int(9.7) returns 9

Math.Round(2.7) is 3

Page 72: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 72

Integer Data Type

• Variables of type Double can be assignedboth whole numbers and numbers withdecimals.

• 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 73: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 73

Multiple Declarations

Dim a, b As Double

Two other types of multiple-declarationstatements are

Dim a As Double, b As IntegerDim c As Double = 2, b As Integer = 5

Page 74: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 74

Parentheses

• Parentheses should be used liberally innumeric expressions.

• In the absence of parentheses, theoperations are carried out in the followingorder: ^, * and /, + and -.

Page 75: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 75

Three Types of Errors

• Syntax error

• Run-time error

• Logic error

Page 76: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 76

Some Types of Syntax Errors

• Misspellings lstBox.Itms.Add(3)

• Omissions lstBox.Items.Add(2 + )

• Incorrect punctuation Dim m; n As Integer

Page 77: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 77

A Type of Run-time Error

• Overflow error

Dim numVar As Integer = 1000000numVar = numVar * numVar

Page 78: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 78

A Logical Error

Dim average As DoubleDim m As Double = 5Dim n As Double = 10average = m + n / 2

Value of average will be 10. Should be 7.5.

Page 79: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 79

3.4 Strings

• Variables and Strings

• Using Text Boxes for Input and Output

• Concatenation

• String Properties and Methods:

•Substring•IndexOf

•ToLower•Trim

•ToUpper•Length

Page 80: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 80

Strings continued

• The Empty String

• Initial Value of a String

• Option Strict

• Internal Documentation

• Line-Continuation Character

Page 81: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 81

String Literal

A string literal is a sequence of

characters surrounded by quotation marks.

Examples:"hello"

"123-45-6789""#ab cde?"

Page 82: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 82

String Variable

A string variable is a name to which a

string value can be assigned.

Examples:

country

ssn

word

firstName

Page 83: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 83

String Variable

• Declaration:Dim firstName As String

Variable nameData type

• Assignment:firstName = "Fred"

Page 84: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 84

String Variable

You can declare a string variable and

assign it a value at the same time.

Dim firstName As String = "Fred"

Page 85: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 85

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 86: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 86

String Variable

You can assign the value of one string

variable to another.

Dim strVar1 As String = "Hello"Dim strVar2 As String = "Goodbye"strVar2 = strVar1lstOutput.Items.Add(strVar2)

Output: Hello

Page 87: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 87

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 88: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 88

Using Text Boxes for Input andOutput

• The contents of a text box is always a string

• Input example strVar = txtBox.Text

• Output example txtBox.Text = strVar

Page 89: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 89

Data Conversion

• Because the contents of a text box isalways a string, sometimes you mustconvert 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 90: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 90

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 91: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 91

Appending

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

• Or as a shortcutvar &= str

Page 92: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 92

Appending ExampleDim var As String = "Good"var &= "bye"txtBox.Text = var

OUTPUT: Goodbye

Page 93: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 93

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.

Page 94: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 94

Positions in a StringPositions 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 95: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 95

Substring MethodLet 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 96: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 96

IndexOf MethodLet 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 97: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 97

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

called the empty string or the zero-lengthstring.

• The statement lstBox.Items.Add("") skips aline in the list box.

• The contents of a text box can be cleared witheither the statement

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

Page 98: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 98

Initial Value of a String

• By default the initial value is Nothing

• Strings can be given a different initialvalue as follows:

Dim today As String = "Monday"

Page 99: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 99

Option Strict

• Visual Basic allows numeric variables tobe assigned strings and vice versa, apoor programming practice.

• To turn this feature off, put the followingstatement at the very top of the codewindow

Option Strict On

Page 100: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 100

Option Strict On for All Programs

• Select Options from the Tools menu

• In left pane, expand Projects andSolution

• Select VB Defaults

• Set Option Strict to On

Page 101: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 101

With Option Strict OnDim dblVar As Double, intVar As IntegerDim strVar As String

Not Valid: Replace with:intVar = dblVar intVar = CInt(dblVar)dblVar = strVar dblVar = CDbl(strVar)strVar = intVar strVar = CStr(intVar)

Page 102: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 102

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 103: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 103

Internal Documentation

1. Other people can easily understand theprogram.

2. You can understand the program whenyou read it later.

3. Long programs are easier to readbecause the purposes of individualpieces can be determined at a glance.

Page 104: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 104

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 105: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 105

Line-Continuation Character

• A long line of code can be continued onanother line by using an underscore (_)preceded by a space

msg = "I'm going to make " & _ "him an offer he can't refuse."

Page 106: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 106

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 107: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 107

Formatting Output with FormatFunctions

18%FormatPercent(0.183, 0)

$12,345.63FormatCurrency(12345.628, 2)

12,345.6FormatNumber(12345.628, 1)

String ValueFunction

Page 108: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 108

Formatting Output with Zones

• Use a fixed-width font such as Courier New

• Divide the characters into zones with aformat string.

Dim fmtStr As String = "{0, 15}{1, 10}{2, 8}"lstOutput.Items.Add(String.Format(fmtStr, _ data0, data1, data2))

Page 109: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 109

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 110: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 110

Zone Formatting Symbols

FormatPercent(data, r):Pr

FormatCurrency(data, r):Cr

FormatNumber(data, r):Nr

Effect on zoneSymbols: N, C, and P

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

Page 111: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 111

Reading Data from Files

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

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

Page 112: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 112

Sample File: PAYROLL.TXT

Mike Jones

7.35

35

John Smith

6.75

33

Name

Hourly wage

Number of hours worked

Page 113: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 113

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 114: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 114

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 115: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 115

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 116: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 116

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 117: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 117

Getting Input from an InputDialog Box

stringVar = InputBox(prompt, title)fileName = InputBox("Enter the name " _ & "of the file containing the " & _ "information.", "Name of File")

Title

Prompt

Page 118: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 118

Using a Message Dialog Boxfor OutputMsgBox(prompt, 0, title)

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

Title

Prompt

Page 119: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 119

Masked Text Box

Similar to an ordinary text box, but hasa Mask property that restricts what canbe typed into the masked text box.

Page 120: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 120

Input Mask Dialog Box

Page 121: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 121

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 122: Introducing Visual Basic…...Initial Visual Basic Screen Chapter 3 - VB 2005 by Schneider 10 Toolbox Chapter 3 - VB 2005 by Schneider 11 3 Ways to Place a Control from the Toolbox

Chapter 3 - VB 2005 by Schneider 122

Sample Masks

State abbreviation: LL

Phone number: 000-0000

Social Security Number: 000-00-0000

License plate: &&&&&&