5 Chapter 5 Basics of File Input and

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

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 2

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 3

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

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

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

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

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

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

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

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

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

Create the Sticky Pad Project using the Common Dialog Control Hands-On Exercise 1 n n n n 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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 13

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

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 15

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 16

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 17

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 18

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

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 Free. File function is used to obtain the next available file number Example: Open Filename For Input As #1 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 20

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 21

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

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) Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 23

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

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

The File System Object Ø FSO - new feature of Visual Basic 6. 0 Ø Provides and object based model for working with drives, folders, and files n n n File. System. Object 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 Text. Stream object - enables you to read and write text file Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 26

The File System Object Useful Functions and Methods n Create. Object function n Open. Text. File method n Text. Stream object methods and properties Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 27

FSO Example Code Sub Open. Text. File() Const For. Reading = 1 Const For. Writing = 2 Const For. Appending = 3 Dim fso ‘File System Object Dim textfile ‘textstream file Set fso = Create. Object(“Scripting. File. System. Object”) Set textfile = fso. Open. Text. File(“C; testfile. txt”, _ For. Writing, True, Tristate. False) textfile. Write “This is a test. ” textfile. Close End Sub Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 28

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 29

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

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

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

Working with Strings Len (Length of String) function - finds the length of a string In. Str (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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 33

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 34

The In. Str Function In. Str(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 = In. Str(Pathname, “”) Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 35

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

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

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: Middle. Word = Mid(string, start, 6) Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 38

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

The Print. Form Function Form. Name. Printform Where Form. Name is a form object. If this object is omitted, the selected form is printed. Example: frm. Main. Print. Form Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 40

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 41

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

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

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

Working with the Printer Object Private Sub mnu. Print_Click() ‘Purpose: Print note using system defaults On Error Go. To Print. Error Printer. Print ; ‘initialize printer Printer. Print Me. Caption ‘send to printer Print vb. Cr. Lf; ‘send linefeed Print Me. txt. Note ‘send textbox Printer. End. Doc ‘print and end Exit Sub Print. Error: Msg. Box “Can’t print sticky note file “& Pathname End Sub Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 45

Set Printer Object Properties Printer. property Where Printer is the default print Property is one of the following Font. Name, Font. Size Font. Bold, Font. Italic Scale. Left Scale. Top Page Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 46

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

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, txt. Note Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 48

Set the Flags Property Common. Dialog. Name. Flags Flag. Constant Where Common. Dialog. Name is the name property assigned to the common dialog control Flags is the method Flag. Constant is one or more of the following cdl. PDPrint. Setup ‘display print setup cdl. PDHelp. Button ‘display help button Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 49

Set More Properties for Printing Common. Dialog. Name. Method Where Common. Dialog. Name is the name property assigned to the common dialog control Method is one of the following Copies ‘number of copies to print From. Page ‘page to start printing To. Page ‘page to stop printing h. DC ‘selected printer Printer. Default = Setting ‘if true prints with current printer Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 50

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

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 52

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 #, Close, Print # statements and functions for reading and writing files Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 53

Summary Ø File System Object (FSO) is an objectbased 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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 54

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 Exploring MS Visual Basic 6 Copyright 1999 Prentice-Hall, Inc. 55

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