Tutorial 9 Sequential Access Files and Printing Tutorial

  • Slides: 34
Download presentation
Tutorial 9 Sequential Access Files and Printing Tutorial 9: Sequential Access Files and Printing

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

Sequential Access Files Lesson A Objectives After completing this lesson, you will be able to: § Declare Stream. Reader and Stream. Writer 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

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

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 Stream. Writer or

Procedure for Using a Sequential Access File 1. Declare either a Stream. Writer or Stream. Reader object variable 2. Create a Stream. Writer or Stream. Reader object by opening a file; assign the object’s address to the object variable declared in Step 1 3. Use the Stream. Writer object to write one or more lines of text to the file, or use the Stream. Reader object to read one or more lines of text from the file 4. Use the Stream. Writer or Stream. Reader object to close the file Tutorial 9: Sequential Access Files and Printing 5

Using Stream. Writer and Stream. Reader Objects In Visual Basic. NET § Use a

Using Stream. Writer and Stream. Reader Objects In Visual Basic. NET § Use a Stream. Writer 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 Stream. Reader 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 6

Opening a Sequential File § Refer to Figure 9 -7 in the textbook, which

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 §Create. Text method – Creates a file for writing a text stream obj. Stream. Writer = System. IO. File. Create. Text(“memo. txt”) §Append. Text method – Opens a file appending text to the end of the file obj. Stream. Writer = System. IO. File. Append. Text(“sales. txt”) §Open. Text method – Opens a file to read a stream of text obj. Stream. Reader = System. IO. File. Open. Text(“a: reportspay. txt”) Tutorial 9: Sequential Access Files and Printing 7

Does the file exist? § Avoid errors by using the Exists method If System.

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 8

Writing to a Sequential File Write method Leaves the file pointer after last character

Writing to a Sequential File Write method Leaves the file pointer after last character obj. Stream. Writer. Write(“Good”) obj. Stream. Writer. Write(“ Morning”) ‘ Writes on the same line Good Morning Write. Line method Appends the line terminator to the end of line and leaves the file pointer ready for next line obj. Stream. Writer. Write. Line(“Good”) obj. Stream. Writer. Write. Line(“ Morning”) ’ Each is on a separate line Good Morning Tutorial 9: Sequential Access Files and Printing 9

Formatting String Data § The Space function inserts spaces obj. Stream. Writer. Write(Space(10) &

Formatting String Data § The Space function inserts spaces obj. Stream. Writer. Write(Space(10) & “A” & Space(5) & “B” § Pad. Left – Pads a string on the left with specified character Dim sng. Net. Pay As Single = 767. 89, str. Net As String str. Net = Format. Currency (sng. Net. Pay) str. Net = str. Net. Pad. Left(15, “*”) Results in “****$767. 89” § Pad. Right – Pads a string on the right with specified character Dim str. Name As String = “Sue” str. Name = str. Name. Pad. Right(10) Results in “Sue ” (the string “Sue” and seven spaces) Tutorial 9: Sequential Access Files and Printing 10

Aligning Data in a Sequential File For int. Region = 1 To 3 str.

Aligning Data in a Sequential File For int. Region = 1 To 3 str. Sales = Input. Box(“Sales amount”, “Sales”) str. Sales = Format(str. Sales, “standard”) obj. Stream. Writer. Write. Line(str. Sales. Pad. Left(8) ) Next int. Region 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 11

Reading from a Sequential File § Read. Line Method – Read a line of

Reading from a Sequential File § Read. Line Method – Read a line of text from the file str. Line = obj. Stream. Reader. Read. Line() § Peek method – Looks to see if there is another character to read Do Until obj. Stream. Reader. Peek = -1 § Close method – Close the file when you are finished obj. Stream. Reader. Close() obj. Stream. Writer. Close() Tutorial 9: Sequential Access Files and Printing 12

Using a Date. Time. Picker Control Lesson B Objectives After completing this lesson, you

Using a Date. Time. Picker Control Lesson B Objectives After completing this lesson, you will be able to: § Add a Date. Time. Picker control to a form § Control the appearance of a Date. Time. Picker control § Format the text that appears in a Date. Time. Picker control § Set and retrieve the information stored in a Date. Time. Picker control § Retrieve the system date and time § Display a form immediately Tutorial 9: Sequential Access Files and Printing 13

Adding a Date. Time. Picker Control to a Form § The Date. Time. Picker

Adding a Date. Time. Picker Control to a Form § The Date. Time. Picker 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 14

The Show. Up. Down Property § The Date. Time. Picker control’s Show. Up. Down

The Show. Up. Down Property § The Date. Time. Picker control’s Show. Up. Down 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 Show. Up. Down property is set to True, up and down arrow buttons appear on the control Tutorial 9: Sequential Access Files and Printing 15

The Format Property § You can use the Format property to control the format

The Format Property § You can use the Format property to control the format (style) of the date or time displayed in the Date. Time. Picker control Tutorial 9: Sequential Access Files and Printing 16

Tutorial 9: Sequential Access Files and Printing 17

Tutorial 9: Sequential Access Files and Printing 17

The Value Property § When you add a Date. Time. Picker control to a

The Value Property § When you add a Date. Time. Picker 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 Date. Time. Picker control’s Value property § You can verify that fact by viewing the Value property in the Properties list § The Date. Time object is simply an object that represents a date and an optional time Tutorial 9: Sequential Access Files and Printing 18

The Value Property Example Results Me. Pay. Date. Time. Picker. Value 11/12/2004 9: 07:

The Value Property Example Results Me. Pay. Date. Time. Picker. Value 11/12/2004 9: 07: 00 AM Me. Pay. Date. Time. Picker. Value. To. Long. Date. String Friday, November 12, 2004 Me. Pay. Date. Time. Picker. Value. To. Short. Date. String 11/12/2004 Me. Pay. Date. Time. Picker. Value. To. Long. Time. String 9: 08: 00 AM Me. Pay. Date. Time. Picker. Value. To. Short. Time. String 9: 08 AM Tutorial 9: Sequential Access Files and Printing 19

Using the Date. Time Object objectname. Value = New Date. Time(year, month, day[, hour,

Using the Date. Time Object objectname. Value = New Date. Time(year, month, day[, hour, minute, second]) Me. Pay. Date. Time. Picker. Value = New Date. Time(2004, 2, 3) Me. Pay. Date. Time. Picker. Value = New Date. Time(2004, 9, 7, 5, 30, 18) Me. Pay. Date. Time. Picker. Value = New Date. Time(2004, 1, 6, 17, 30, 18) Tutorial 9: Sequential Access Files and Printing 20

Retrieving the Information Stored in the Value Property Example using “ 11/12/2004 9: 07

Retrieving the Information Stored in the Value Property Example using “ 11/12/2004 9: 07 AM” Results Me. Pay. Date. Time. Picker. Value. Month 11 Me. Pay. Date. Time. Picker. Value. Day 12 Me. Pay. Date. Time. Picker. Value. Year 2004 Me. Pay. Date. Time. Picker. Value. Day. Of. Week 5 Me. Pay. Date. Time. Picker. Value. Day. Of. Week. To. String Friday Me. Pay. Date. Time. Picker. Value. Hour 9 Me. Pay. Date. Time. Picker. Value. Minute 7 Me. Pay. Date. Time. Picker. Value. Second 0 Tutorial 9: Sequential Access Files and Printing 21

Text Property § The text that appears in a Date. Time. Picker control is

Text Property § The text that appears in a Date. Time. Picker 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 22

Tutorial 9: Sequential Access Files and Printing 23

Tutorial 9: Sequential Access Files and Printing 23

Retrieving the System Date and Time § The system date is the current date

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 Time. Of. Day[. methodname], where methodname (which is optional) is To. Long. Time. String, To. Short. Time. String, or To. String(formatting characters) Tutorial 9: Sequential Access Files and Printing 24

Coding the Carriage. Form Load Event Procedure § The pseudocode for the Carriage. Form

Coding the Carriage. Form Load Event Procedure § The pseudocode for the Carriage. Form 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 25

Tutorial 9: Sequential Access Files and Printing 26

Tutorial 9: Sequential Access Files and Printing 26

Coding the Add. Button Click Event Procedure § The pseudocode for the Add. Button

Coding the Add. Button Click Event Procedure § The pseudocode for the Add. Button Click event is shown in Figure 9 -40 of the textbook Tutorial 9: Sequential Access Files and Printing 27

Tutorial 9: Sequential Access Files and Printing 28

Tutorial 9: Sequential Access Files and Printing 28

Completing the Carriage House Application Lesson C Objective After completing this lesson, you will

Completing the Carriage House Application Lesson C Objective After completing this lesson, you will be able to: Add a Print. Document control to a form Print text using the Print and e. Graphics. Draw. String methods Code a Print. Document control’s Print. Page event procedure Tutorial 9: Sequential Access Files and Printing 29

Adding a Print. Document Control to the Form § Before you can print a

Adding a Print. Document Control to the Form § Before you can print a document within a Windows application, you need to add a Print. Document control to the form Tutorial 9: Sequential Access Files and Printing 30

Coding the Print Report Button’s Click Event Procedure § The Print method causes the

Coding the Print Report Button’s Click Event Procedure § The Print method causes the Print. Document control’s Print. Page event to occur § You use the Print. Page 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 31

Tutorial 9: Sequential Access Files and Printing 32

Tutorial 9: Sequential Access Files and Printing 32

Coding the Print. Page Event Procedure § In this case, the Print. Page event

Coding the Print. Page Event Procedure § In this case, the Print. Page 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 33

The e. Graphics. Draw. String Method § You use the e. Graphics. Draw. String

The e. Graphics. Draw. String Method § You use the e. Graphics. Draw. String 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 34