CS 4 Intro to Programming using Visual Basic - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CS 4 Intro to Programming using Visual Basic

Description:

CS 4 Intro to Programming using Visual Basic – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 19
Provided by: cwy73
Category:

less

Transcript and Presenter's Notes

Title: CS 4 Intro to Programming using Visual Basic


1
CS 4 Intro to Programming using Visual Basic
  • Input Output
  • Patchrawat UthaisombutUniversity of Pittsburgh

based on lecture notes by D. Schneider
2
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

3
Formatting Output with Format Functions
4
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, 151, 102, 8"
  • lstOutput.Items.Add(String.Format(fmtStr, _
  • data0, data1, data2))

5
Formatting Output with Zones
  • Dim fmtStr As String "0, -151, 102, 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.

6
Zone Formatting Symbols
Dim fmtStr As String "0,15N11,10C22,8P0
"
7
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.

8
Sample File PAYROLL.TXT
  • Mike Jones
  • 7.35
  • 35
  • John Smith
  • 6.75
  • 33

Name
Hourly wage
Number of hours worked
9
Steps to Use StreamReader
  • Execute 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)

10
Steps to Use StreamReader
  • Read 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 from
  • the file, terminate the communications link
  • readerVar.Close()

11
Example using StreamReader
  • Dim name As String
  • Dim wage, hours As Double
  • Dim sr As IO.StreamReader _
  • IO.File.OpenText("PAYROLL.TXT")
  • name sr.ReadLine
  • wage CDbl(sr.ReadLine)
  • hours CDbl(sr.ReadLine)
  • lstBox.Items.Add(name " " wage hours)
  • OUTPUT Mike Jones 257.25

12
Comment on Example
  • Consider
  • lstBox.Items.Add(name " " wage hours)
  • The ampersand automatically converted
  • wage hours into a string before concatenating.
  • We didnt have to convert wage hours with CStr.

13
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
14
Using a Message Dialog Box for Output
  • MsgBox(prompt, 0, title)
  • MsgBox("Nice try, but no cigar.", 0, _
  • "Consolation")

Title
Prompt
15
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.
16
Input Mask Dialog Box
17
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.
18
Sample Masks
State abbreviation LL Phone number
000-0000 Social Security Number
000-00-0000 License plate
Write a Comment
User Comments (0)
About PowerShow.com