Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Handling.

Similar presentations


Presentation on theme: "File Handling."— Presentation transcript:

1 File Handling

2 Data Files Programs that can only get data from the user interface are limited. data must be entered each time. only small amounts of data can be processed. Both limitations can be circumvented by storing data in text files.

3 Data Files A text file is one that contains only ASCII characters.
Operations that can be performed on files: Read Write Append

4 Microsoft Common Dialog Controls
Files as Objects Dealing with files will require specifying their names and paths. To facilitate this the standard Windows Dialog boxes can be used. But VB6 doesn’t automatically have access to these, so before we can deal with files we need to add some functionality. We need to add the Microsoft Common Dialog Controls

5 Adding Components Click on Project Components…

6 Adding Controls Scroll down to Microsoft Common Dialog Control 6.0
Check it Click OK

7 Including Dialog Controls
Observe that the icon representing the control for creating the standard Microsoft Windows dialog windows has been added to the toolbox.

8 Including Dialog Contols
Like any other control, The Common Dialog contol must be added to the interface for a project to use it. Simply click the icon in the toolbox And click on the form. The control cannot be resized because it does not appear in the interface.

9 Common Dialog Control The Common Dialog Control allows VB6 projects to use a set of dialog boxes that are common in Windows applications. The methods in the Common Dialog Control include routines to perform various tasks: Change a font Change colours Open a file Print Provide help Save a file

10 Dialog Control Properties
Like all controls, the Common Dialog has properties: Filter –specifies file types shown InitDir – specifies the initial directory CancelError – determines how clicking “Cancel” will be treated Flags – sets various conditions for the dialog

11 Dialog Control Properties
For a Dialog Control called Test, a program could set properties at runtime like these… cdlTest.Filter = "Text files|*.txt" cdlTest.Flags = cdlOFNFileMustExist cdlTest.InitDir = "C:\My Music"

12 Common Dialog Control The Dialog Control provides access to the common dialogs, but using these dialogs requires further tools. To read a text file, for example, new classes are needed: FileSystemObject TextStream These classes have properties and methods.

13 TextStream Methods Close Read ReadAll ReadLine Skip SkipLine Write
WriteBlankLines WriteLine

14 Incorporating New References
Adding a new collection of classes simply makes them available. We also need to create References to them.

15 Incorporating New References
Click on Project, then References… This dialog appears.

16 Incorporating New References
Scroll down and Select Microsoft Scripting Runtime

17 Dealing with Files This gives us the tools, now we need to write the code to use them.

18 Reading a File To read data from a file:
create a reference to an object of the FileSystemObject class Dim aFile As FileSystemObject create a reference to an object of the TextStream class Dim FileContent As TextStream

19 Reading a File use the common dialog control to obtain the file name
cdlTest.ShowOpen create a new FileSystemObject object Set aFile = New FileSystemObject use the OpenTextFile method on the FileSystemObject to obtain a TextStream object Set FileContent = aFile.OpenTextFile(cdlTest.FileName)

20 Reading a File use the ReadAll method on the TextStream object to obtain the file contents as a string txtFileContent.Text = FileContent.ReadAll close the file FileContent.Close

21 Reading a File Private Sub cmdOpenFile_Click()
Dim aFile As FileSystemObject Dim FileContent As TextStream ' cdlTest.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*" cdlTest.Flags = cdlOFNFileMustExist cdlTest.ShowOpen Set aFile = New FileSystemObject Set FileContent = aFile.OpenTextFile(cdlTest.FileName) txtFileContent.Text = FileContent.ReadAll FileContent.Close Exit Sub

22 Error Handling It’s always best to handle errors with program code rather than system messages (and aborted programs). VB6 provides an easy way to deal with errors. Private Sub ReadAFile(…) ‘branch to error handler if error occurs OnError GoTo ErrorHandler ‘regular subroutine Exit Sub ‘write error handling code ErrorHandler: ‘code to deal with errors End Sub

23 A Complete Example Private Sub cmdOpenFile_Click()
On Error GoTo ErrorHandler Dim aFile As FileSystemObject Dim FileContent As TextStream cdlTest.ShowOpen Set aFile = New FileSystemObject Set FileContent = aFile.OpenTextFile(cdlTest.FileName) txtFileContent.Text = FileContent.ReadAll FileContent.Close Exit Sub ErrorHandler: 'user pressed Cancel - do nothing Dim messg As String messg = Str(Err.Number) & ": " & Err.Description MsgBox (messg) End Sub


Download ppt "File Handling."

Similar presentations


Ads by Google