Tutorial 9 Sequential Access Files and Printing

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Advertisements

Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Chapter 11: Classes and Objects
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 9: Sequential Access Files and Printing
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Chapter 7: Sub and Function Procedures
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Chapter 9 Files I/O: Files, Records and Fields. Basics of File Input and Output Have created both input and outputs from programs. Persistent data: What.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Tutorial 61 List Box Control Can be used to display a set of choices from which the user can select only one You also can create multi-selection list boxes.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Creating Sequential Files, Adding Data, & Deleting Data.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
Working with option button, check box, and list box controls Visual Basic for Applications 13.
Date Variables Visual Basic for Applications 5. Objectives n In this tutorial, you will learn how to: n Reserve a Date variable n Use an assignment statement.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Chapter 6: The Repetition Structure
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 8: Manipulating Strings
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Tutorial 8: Manipulating Strings1 Tutorial 8 Manipulating Strings.
Visual Basic.NET BASICS Lesson 14 Menus and Printing.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
Chapter 14: Sequential Access Files
Excel Tutorial 8 Developing an Excel Application
14 Shipping Time App Using Dates and Timers
Tutorial 14 – Shipping Time Application Using DateTimes and Timers
Microsoft Visual Basic 2008: Reloaded Third Edition
Microsoft Visual Basic 2005: Reloaded Second Edition
Reading & writing to files
Variables and Arithmetic Operations
MODULE 7 Microsoft Access 2010
Topics Introduction to File Input and Output
CIS16 Application Development and Programming using Visual Basic.net
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter 3.5 Input and Output
Text / Serial / Sequential Files
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
Presentation transcript:

Tutorial 9 Sequential Access Files and Printing

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

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

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

Procedure for Using a Sequential Access File Declare either a StreamWriter or StreamReader object variable Create a StreamWriter or StreamReader object by opening a file; assign the object’s address to the object variable declared in Step 1 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 Use the StreamWriter or StreamReader object to close the file Tutorial 9: Sequential Access Files and Printing

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

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

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

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

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

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

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 Close method – Close the file when you are finished objStreamReader.Close() objStreamWriter.Close() Tutorial 9: Sequential Access Files and Printing

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

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

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

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

Tutorial 9: Sequential Access Files and Printing

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

The Value Property Example Results Me.PayDateTimePicker.Value 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

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

Retrieving the Information Stored in the Value Property Example using “11/12/2004 9:07 AM” Results Me.PayDateTimePicker.Value.Month 11 Me.PayDateTimePicker.Value.Day 12 Me.PayDateTimePicker.Value.Year 2004 Me.PayDateTimePicker.Value.DayOfWeek 5 Me.PayDateTimePicker.Value.DayOfWeek.ToString Friday Me.PayDateTimePicker.Value.Hour 9 Me.PayDateTimePicker.Value.Minute 7 Me.PayDateTimePicker.Value.Second Tutorial 9: Sequential Access Files and Printing

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

Tutorial 9: Sequential Access Files and Printing

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

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

Tutorial 9: Sequential Access Files and Printing

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

Tutorial 9: Sequential Access Files and Printing

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

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

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

Tutorial 9: Sequential Access Files and Printing

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

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