Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Basics of File Input and Output Reading, Writing, and Printing Text Files 5 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall,

Similar presentations


Presentation on theme: "Chapter 5 Basics of File Input and Output Reading, Writing, and Printing Text Files 5 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall,"— Presentation transcript:

1 Chapter 5 Basics of File Input and Output Reading, Writing, and Printing Text Files 5 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton

2 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.2 Objectives... 1. Use Common Dialog control 2. Display dialog boxes to open and save files, set printer options, and select colors and fonts 3. Implement file I/O operations such as open, close, save and print 4. Write code to open a file and display it, then save changes to the file 5. Text, binary, RTF and HTML file formats

3 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.3 Objectives 6. Sequential vs. Binary vs. Random file access 7. Use File System Object (FSO) model to process files 8. Use string functions 9. Print form and text from your project 10. Write code to handle errors and avoid runtime error crashes

4 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.4 Windows Dialog Boxes  Open Dialog Box  Save As Dialog Box  Print Dialog Box  Color Dialog Box  Font Dialog Box

5 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.5 Common Dialog Control CommonDialogName.Method Where CommonDialogName is the name property assigned to the common dialog control Method is one of the following: ShowOpen displays the Open dialog box ShowSave displays the Save As dialog box ShowColor displays the Color dialog box ShowFont display the Font dialog box ShowPrinter displays the Print dialog box ShowHelp starts the Windows Help engine

6 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.6 Open Dialog Box  Common Dialog control  ShowOpen method

7 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.7 Save As Dialog Box  Common Dialog control  ShowSave method

8 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.8 Print Dialog Box  Common Dialog control  ShowPrinter method

9 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.9 Color Dialog Box  Common Dialog control  ShowColor method

10 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.10 Font Dialog Box  Common Dialog control  ShowFont method

11 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.11 The Common Dialog Control  Common dialog control provides access to Windows’ most commonly used dialog boxes  First add the Common dialog ActiveX control to the toolbox  Filter property selects the files to display based on file extensions Example: “Text file (*.txt) | *.txt”

12 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.12 The Sticky Pad Project

13 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.13 Create the Sticky Pad Project using the Common Dialog Control Hands-On Exercise 1 Create and test a new form Add the common dialog control Create the menus Add code for menus Display the Open dialog box Display the Save As dialog box Add and test code for the Close Note menu Add Banner comments

14 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.14 Instances and Classes Class formal definition of an object Analogy - gingerbread man cookie cutter Instance one of the set of objects that belongs to a class Analogy - any of the gingerbread men cookies

15 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.15 Types of Files File - block of information stored on disk or another media Text file - file that contains lines of written information that can be sent directly to the screen or printer Binary file - file that contains bits that do not necessarily represent printable text Examples: Word file, machine language file

16 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.16 Types of Files RTF file (Rich Text Format) - standard developed by Microsoft to format documents HTML (Hypertext Markup Language) - used to define formatting for web page files Note: Rich Text Box control is only available in the Professional and Enterprise editions

17 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.17 File Access Methods I/O (Input/Output) - refers to an operation or program that enters or extracts data from a computer Access - read data from or write data to a storage device

18 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.18 File Access Sequential access - used to read and write text files in continuous blocks Random access - used to read and write text or binary files structured as fixed-length records Binary access - used to read and write files that are structured according to an application’s specifications

19 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.19 Reading and Writing Files Statements and Functions  Open statement  Input function  Input # statement  Close statement  LOF (Length of File) function  Print # statement

20 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.20 The Open Statement Open pathname For mode As #filenumber Where pathname is a string that specifies a filename, and may include the folder and drive mode must be one of the following: Append, Binary, Input, Output, or Random filenumber is a valid file number from 1 to 511 Note: The FreeFile function is used to obtain the next available file number Example: Open Filename For Input As #1

21 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.21 The Close Statement Close #filenumber Where filenumber is any valid file number. If the file number is omitted all active files are closed. Example: Close #1

22 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.22 The LOF Function LOF(filenumber) Where Filenumber is a valid file number Example: FileLength = LOF(1)

23 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.23 The Input Function Input(charnumber, filenumber) Where Charnumber is the number of characters to read Filenumber is any valid file number Example: String = Input(Number, 1)

24 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.24 The Print # Statement Print #filenumber, outputname Where filenumber is any valid file number outputname is the object to be printed Example: Print #1, txtNote

25 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.25 Open, Read and Write a File Sub FileIO() Dim Pathname as String Dim FileLength As long Open Pathname For Input As #1 FileLength = LOF(1) txtFile = Input(Filelength, 1) Close #1 ‘Do other stuff here Open Pathname For Output As #1 Print #1, txtNote Close #1 End Sub

26 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.26 The File System Object  FSO - new feature of Visual Basic 6.0  Provides and object based model for working with drives, folders, and files FileSystemObject object - primary object Drive object - info about a drive such as space Folder object - create, change, move and delete Files object - create, delete, change and move TextStream object - enables you to read and write text file

27 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.27 The File System Object Useful Functions and Methods CreateObject function OpenTextFile method TextStream object methods and properties

28 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.28 FSO Example Code Sub OpenTextFile() Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 3 Dim fso ‘File System Object Dim textfile‘textstream file Set fso = CreateObject(“Scripting.FileSystemObject”) Set textfile = fso.OpenTextFile(“C;\testfile.txt”,_ ForWriting, True, TristateFalse) textfile.Write “This is a test.” textfile.Close End Sub

29 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.29 Handling I/O Errors Bug - mistake in a computer program Compiler errors - occur when the rules of the Visual Basic programming language were not followed Runtime errors - occur when executing a program Logic errors - occur when the program makes a mistake in understanding the problem or implements solution incorrectly

30 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.30 Handing I/O Errors On Error GoTo LineName must be the first executable statement in a procedure If error is encountered, the program will execute the code immediately after the LineName statement Place an Exit Sub statement immediately before LineName statement

31 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.31 Handling I/O Errors Sub FileIO() ‘Purpose: Error handling example Dim Pathname As String On Error GoTo ErrorHandler‘set up handler ‘Insert code to open, save, or print file here ‘No errors encountered here Exit Sub ErrorHandler: ‘Display a message box and exit procedure MsgBox “Cannot complete file operation” Exit Sub End Sub

32 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.32 Add File I/O to Sticky Pad Project Hands-On Exercise 2 Open the Sticky Pad project Add code to open a text file Add code to save a text file Add code to display the font dialog box Test the font dialog box Add code to display the color dialog box

33 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.33 Working with Strings Len (Length of String) function - finds the length of a string InStr (In String) function - searches a string for a substring Left function - finds the leftmost characters of a string Right function - finds the rightmost characters of a string Mid function - finds characters in the middle of a string

34 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.34 The Len Function Len (string | varname) Where String is any valid string expression Varname is any valid variable name Either string or a varname is given, not both Example: If Len(Filename) = 0 Then Exit Sub

35 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.35 The InStr Function InStr(startposition, string, substring) Where startposition sets the starting position for the search string is the string you are searching substring is the substring you are looking for Example: Position = InStr(Pathname, “\”)

36 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.36 The Left Function Left(string, length) Where string is any valid string expression length indicates the number of characters to return Example: FirstEight = Left(string, 8)

37 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.37 The Right Function Right(string, length) Where string is any valid string expression length indicates the number of characters to return Example: LastEight = Right(string, 8)

38 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.38 The Mid Function Mid(string, startposition, length) Where string is any valid string expression startposition sets the starting position for extracting the substring length indicates the number of characters to return Example: MiddleWord = Mid(string, start, 6)

39 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.39 Working with Printers We can print 3 different ways We create a form, then print the form Send text and graphics to the default printer using the system defaults Display the Print dialog box to offer printing options, set the printer options, then send to the user’s choice of printers and print

40 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.40 The PrintForm Function FormName.Printform Where FormName is a form object. If this object is omitted, the selected form is printed. Example: frmMain.PrintForm

41 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.41 Working with Printers  Print Form is a quick and easy way to print  Prints at screen resolution 72 - 96 dots per inch  Printers are capable of resolutions of 300, 600, 1200 or more dots per inch  So, print out is low quality

42 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.42 The Printers Collection Set as the default printer Another printer A fax card A file stored on disk

43 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.43 Working with the Printer Object Printer Object device-independent drawing space that supports text and graphics Send signal to start print job Place text or graphics on pages Print the page Start a new page and continue until finished

44 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.44 The Printer Object Printer.method Where Printer is the default print Method is one of the following Print Objectname New Page End Doc KillDoc

45 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.45 Working with the Printer Object Private Sub mnuPrint_Click() ‘Purpose: Print note using system defaults On Error GoTo PrintError Printer.Print ;‘initialize printer Printer.Print Me.Caption‘send to printer Print.Print vbCrLf;‘send linefeed Print.Print Me.txtNote‘send textbox Printer.EndDoc‘print and end Exit Sub PrintError: MsgBox “Can’t print sticky note file “& Pathname End Sub

46 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.46 Set Printer Object Properties Printer.property Where Printer is the default print Property is one of the following FontName, FontSize FontBold, FontItalic ScaleLeft ScaleTop Page

47 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.47 Display the Print Dialog Box Print SetupPrint

48 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.48 Display the Print Dialog Box Print #filenumber, outputname Where filenumber is any valid file number outputname is the object to be printed Example: Print #1, txtNote

49 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.49 Set the Flags Property CommonDialogName.Flags FlagConstant Where CommonDialogName is the name property assigned to the common dialog control Flags is the method FlagConstant is one or more of the following cdlPDPrintSetup ‘display print setup cdlPDHelpButton‘display help button

50 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.50 Set More Properties for Printing CommonDialogName. Method Where CommonDialogName is the name property assigned to the common dialog control Method is one of the following Copies‘number of copies to print FromPage‘page to start printing ToPage‘page to stop printing hDC‘selected printer PrinterDefault = Setting ‘if true prints with current printer

51 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.51 Add Printing Capability to the Sticky Pad Project Hands-On Exercise 3 Open the Sticky Pad project Display the Print Setup dialog box Display the Print Dialog box Add code and test the print header code Add and test the print footer code Add and test the print text box code

52 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.52 Summary...  Common Dialog Control gives us access to the Windows’ dialog boxes  Open and Save dialog boxes used with files  Print Setup and Print dialog boxes used when printing  Color and Font dialog boxes used with fonts  A class is the formal definition of an object  An instance is one of the objects that belongs to a class  Text, binary, RTF and HTML file formats

53 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.53 Summary...  Sequential access reads and writes sequentially like a cassette tape  Random access reads and writes much like an audio CD  Binary access reads and writes varying record lengths, or no records at all  Open, Input, Input #, Close, Print # statements and functions for reading and writing files

54 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.54 Summary  File System Object (FSO) is an object- based tool to work with drives, folders and files  Bugs can be compiler errors, runtime errors, or logic errors. Use error handling code to avoid runtime errors.  Printer Object is a device independent drawing space  Print Setup and Print dialog boxes give user more control over printing process

55 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.55 Practice with Visual Basic 1. Common Dialog Demo 2. Print Demonstration 3. ANSI Values Demo 4. Clipboard Demo 5. Using Extra Forms 6. Printing Text 7. Processing Text Demonstrations

56 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc.56 Case Studies  Personal Text Editor  The Rich Text Box Control  Ask the Pros  Enhance with Extra Forms


Download ppt "Chapter 5 Basics of File Input and Output Reading, Writing, and Printing Text Files 5 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall,"

Similar presentations


Ads by Google