Tutorial 9 Slides

advertisement

Tutorial 9

Sequential Access Files and Printing

Tutorial 9: Sequential Access Files and Printing 1

Sequential Access Files

Lesson A Objectives

After completing this lesson, you will be able to:

 Declare StreamReader and StreamWriter object variables

 Open a sequential access file

 Determine whether a sequential file exists

 Write information to a sequential access file

 Align the text written to a sequential access file

 Read information from a sequential access file

 Determine whether the computer has finished reading a sequential access file

 Close a sequential access file

Tutorial 9: Sequential Access Files and Printing 2

File Types

 Files to which information is written are called output files , because the files store the output produced by an application

 Files that are read by the computer are called input files , because an application uses the information in these files as input

 Here is a list of different file types:

 Sequential access files

 Random access files

 Binary access files

Tutorial 9: Sequential Access Files and Printing 3

Using Sequential Access Files

 A sequential access file is often referred to as a text file, because it is composed of lines of text

 Sequential access files are similar to cassette tapes in that each line in the file, like each song on a cassette tape, is both stored and retrieved in consecutive order

(sequentially)

Tutorial 9: Sequential Access Files and Printing 4

Procedure for Using a

Sequential Access File

1. Declare either a StreamWriter or StreamReader object variable

2. Create a StreamWriter or StreamReader object by opening a file; assign the object’s address to the object variable declared in Step 1

3. Use the StreamWriter object to write one or more lines of text to the file, or use the StreamReader object to read one or more lines of text from the file

4. Use the StreamWriter or StreamReader object to close the file

Tutorial 9: Sequential Access Files and Printing 5

Example of Using a

Sequential Access File

Dim strLine As String, objStreamReader As System.IO.StreamReader

'determine whether the file.txt file exists

If System.IO.File.Exists("file.txt") = True Then

'create a StreamReader object by opening the file for input objStreamReader = System.IO.File.OpenText("file.txt")

'process the loop instructions until there are no more characters to read

Do Until objStreamReader.Peek = -1

'read a line of text from the file strLine = objStreamReader.ReadLine()

'display the line in a message box

MessageBox.Show(strLine, "File Example", _

MessageBoxButtons.OK, MessageBoxIcon.Information)

Loop

'close the file objStreamReader.Close()

Else

'display an appropriate message

MessageBox.Show("The file.txt file does not exist.", "File Example", _

MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

Tutorial 9: Sequential Access Files and Printing 6

Example of Using a

Sequential Access File

'declare a StreamWriter object variable

Dim objStreamWriter As System.IO.StreamWriter

'create a StreamWriter object by opening the file for append objStreamWriter = System.IO.File.AppendText("file.txt")

'write a line of text to the file objStreamWriter.WriteLine(Me.NameTextBox.Text)

'close the file objStreamWriter.Close()

Tutorial 9: Sequential Access Files and Printing 7

Using StreamWriter and StreamReader Objects

In Visual Basic .NET

 Use a StreamWriter object to write a sequence of characters —referred to as a stream of characters or, more simply, a stream —to a sequential access file

 Use a StreamReader object to read a stream (sequence of characters) from a sequential access file

 Before you create the appropriate object, you first must declare an object variable to store the address of the object in the computer’s internal memory

Tutorial 9: Sequential Access Files and Printing 8

Opening a Sequential File

 Refer to Figure 9-7 in the textbook, which illustrates all of the methods used to open a sequential access file

 CreateText method – Creates a file for writing a text stream objStreamWriter = System.IO.File.CreateText(“memo.txt”)

 AppendText method – Opens a file appending text to the end of the file objStreamWriter = System.IO.File.AppendText(“sales.txt”)

 OpenText method – Opens a file to read a stream of text objStreamReader = System.IO.File.OpenText(“a:\reports\pay.txt”)

Tutorial 9: Sequential Access Files and Printing 9

Does the file exist?

 Avoid errors by using the Exists method

If System.IO.File.Exists(“pay.txt”) Then

 Returns True or False

 The Exists method returns the Boolean value True if

filename exists; otherwise, it returns the Boolean value

False

Tutorial 9: Sequential Access Files and Printing 10

Writing to a Sequential File

Write method Leaves the file pointer after last character objStreamWriter.Write(“Good”) objStreamWriter.Write(“ Morning”) ‘ Writes on the same line

Good Morning

WriteLine method Appends the line terminator to the end of line and leaves the file pointer ready for next line objStreamWriter.WriteLine(“Good”) objStreamWriter.WriteLine(“ Morning”) ’ Each is on a separate line

Good

Morning

Tutorial 9: Sequential Access Files and Printing 11

Formatting String Data

 The Space function inserts spaces objStreamWriter.Write(Space(10) & “A” & Space(5) & “B”

 PadLeft – Pads a string on the left with specified character

Dim sngNetPay As Single = 767.89, strNet As String strNet = FormatCurrency (sngNetPay) strNet = strNet.PadLeft(15, “*”)

Results in “********$767.89”

 PadRight – Pads a string on the right with specified character

Dim strName As String = “Sue” strName = strName.PadRight(10)

Results in “Sue ” (the string “Sue” and seven spaces)

Tutorial 9: Sequential Access Files and Printing 12

Aligning Data in a Sequential File

For intRegion = 1 To 3 strSales = InputBox(“Sales amount”, “Sales”) strSales = Format(strSales, “standard”) objStreamWriter.WriteLine(strSales.PadLeft(8))

Next intRegion

Result (assuming the following sales amounts: 645.75, 1200, 40.80

645.75

1,200.00

40.80

Tutorial 9: Sequential Access Files and Printing 13

Reading from a Sequential File

 ReadLine Method – Read a line of text from the file strLine = objStreamReader.ReadLine()

 Peek method – Looks to see if there is another character to read

Do Until objStreamReader.Peek = -1

 ReadToEnd

Reads the stream from the current position to the end of the stream.

strContent = objStreamReader.ReadToEnd()

 Close method – Close the file when you are finished objStreamReader.Close() objStreamWriter.Close()

Tutorial 9: Sequential Access Files and Printing 14

What to do

 Download and Open the “Seqfile” Zip folder

 Read Tutorial 9 Lesson A

 Run laFile solution

 Read page 571 exercise 14

 Run laModified File solution

 Read page 572 exercise 15

 Run laImports File solution

Tutorial 9: Sequential Access Files and Printing 15

Using a DateTimePicker Control

Lesson B Objectives

After completing this lesson, you will be able to:

 Add a DateTimePicker control to a form

 Control the appearance of a DateTimePicker control

 Format the text that appears in a DateTimePicker control

 Set and retrieve the information stored in a

DateTimePicker control

 Retrieve the system date and time

 Display a form immediately

Tutorial 9: Sequential Access Files and Printing 16

Adding a DateTimePicker

Control to a Form

 The

DateTimePicker control allows the user to select either a date or time, and then displays the selected information in a specified format

Tutorial 9: Sequential Access Files and Printing 17

The ShowUpDown Property

 The DateTimePicker control’s ShowUpDown property determines whether a list arrow button or up and down arrow button appear on the control

 If the property is set to its default value, False, the control contains a list arrow button

 If, on the other hand, the ShowUpDown property is set to True, up and down arrow buttons appear on the control

Tutorial 9: Sequential Access Files and Printing 18

The Format Property

You can use the Format property to control the format (style) of the date or time displayed in the DateTimePicker control

Tutorial 9: Sequential Access Files and Printing 19

Tutorial 9: Sequential Access Files and Printing 20

The Value Property

 When you add a DateTimePicker control to a form, Visual Basic .NET retrieves the current date and time from your computer system’s clock, and assigns both values to the DateTimePicker control’s Value property

 You can verify that fact by viewing the Value property in the Properties list

 The DateTime object is simply an object that represents a date and an optional time

Tutorial 9: Sequential Access Files and Printing 21

The Value Property

Example Results

Me.PayDateTimePicker.Value 11/12/2004 9:07:00 AM

Me.PayDateTimePicker.Value.ToLongDateString Friday, November 12, 2004

Me.PayDateTimePicker.Value.ToShortDateString 11/12/2004

Me.PayDateTimePicker.Value.ToLongTimeString 9:08:00 AM

Me.PayDateTimePicker.Value.ToShortTimeString 9:08 AM

Tutorial 9: Sequential Access Files and Printing 22

Using the DateTime Object

objectname.Value = New DateTime(year, month, day[, hour, minute, second])

Me.PayDateTimePicker.Value = New DateTime(2004, 2, 3)

Me.PayDateTimePicker.Value = New DateTime(2004, 9, 7, 5, 30, 18)

Me.PayDateTimePicker.Value = New DateTime(2004, 1, 6, 17, 30, 18)

Tutorial 9: Sequential Access Files and Printing 23

Retrieving the Information

Stored in the Value Property

Example using “11/12/2004 9:07 AM”

Me.PayDateTimePicker.Value.Month

Me.PayDateTimePicker.Value.Day

Me.PayDateTimePicker.Value.Year

Me.PayDateTimePicker.Value.DayOfWeek

Me.PayDateTimePicker.Value.DayOfWeek.ToString

Me.PayDateTimePicker.Value.Hour

Me.PayDateTimePicker.Value.Minute

Me.PayDateTimePicker.Value.Second

Results

11

12

2004

9

7

0

5

Friday

Tutorial 9: Sequential Access Files and Printing 24

Text Property

 The text that appears in a DateTimePicker control is stored in the control’s Text property

 Visual Basic .NET automatically assigns to the Text property the contents of the Value property formatted using the setting specified in the Format property; this is illustrated in

Figure 9-31 of the textbook

Tutorial 9: Sequential Access Files and Printing 25

Tutorial 9: Sequential Access Files and Printing 26

Retrieving the System Date and Time

 The system date is the current date according to your computer system’s clock

 The system time is the current time according to your computer system

 You can retrieve the system time using the syntax

TimeOfDay[.methodname], where methodname

(which is optional) is ToLongTimeString,

ToShortTimeString, or ToString(formatting characters)

Tutorial 9: Sequential Access Files and Printing 27

Coding the CarriageForm

Load Event Procedure

 The pseudocode for the CarriageForm Load event procedure is shown in Figure 9-34

 The syntax of the form’s Show method is simply Me.Show()

Tutorial 9: Sequential Access Files and Printing 28

Tutorial 9: Sequential Access Files and Printing 29

Coding the AddButton

Click Event Procedure

 The pseudocode for the AddButton Click event is shown in Figure 9-40 of the textbook

Tutorial 9: Sequential Access Files and Printing 30

Tutorial 9: Sequential Access Files and Printing 31

Completing the Carriage

House Application

Lesson C Objective

After completing this lesson, you will be able to:

Add a PrintDocument control to a form

Print text using the Print and e.Graphics.DrawString methods

 Code a PrintDocument control’s PrintPage event procedure

Tutorial 9: Sequential Access Files and Printing 32

Adding a PrintDocument

Control to the Form

 Before you can print a document within a

Windows application, you need to add a

PrintDocument control to the form

Tutorial 9: Sequential Access Files and Printing 33

Coding the Print Report

Button’s Click Event Procedure

 The Print method causes the PrintDocument control’s PrintPage event to occur

 You use the PrintPage event to indicate the information you want to print, as well as how you want the information to appear in the printout

Tutorial 9: Sequential Access Files and Printing 34

Tutorial 9: Sequential Access Files and Printing 35

Coding the PrintPage Event Procedure

 In this case, the PrintPage event procedure should print the contents of the events.txt file in a report format

 The report should contain a report header , which describes the contents of the report, and three columns of information

 The first column should list the event names, the second column the event dates, and the third column the event prices

Tutorial 9: Sequential Access Files and Printing 36

The e.Graphics.DrawString Method

 You use the e.Graphics.DrawString method to print text on the printer

 Some print fonts are proportionally spaced, while others are fixed-spaced, often referred to as mono-spaced

 Fixed-spaced fonts use the same amount of space to print each character

 Proportionally spaced fonts use varying amounts of space to print characters

Tutorial 9: Sequential Access Files and Printing 37

Download