|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
To find out what file the user wants to work with, you check the Common Dialogs FileName property after the user clicks on OK in the dialog box. That property holds the fully qualified (that is, with the path) name of the file to open. If you just want the files name, use the FileTitle property. Heres an example. In this case, well let the user select a file to open, and then display the files name and path in a message box. Add a Common Dialog control to a form and set the controls CancelError property to True so we can check if the user clicked the Cancel button. To check that, we use On Error GoTo: Private Sub Command1_Click() On Error GoTo Cancel ... Cancel: End Sub Then we display the Open dialog box: Private Sub Command1_Click() On Error GoTo Cancel CommonDialog1.ShowOpen ... Cancel: End Sub Finally, assuming the user clicked OK, we display the name of the file the user selected in a message box using the FileName property: Private Sub Command1_Click() On Error GoTo Cancel CommonDialog1.ShowOpen MsgBox "File to open: " & CommonDialog1.FileName Cancel: End Sub When you run this code and click the button, the Open dialog box appears, as in Figure 17.1.
If you make a file selection and click on OK, the Open dialog box closes and the program displays the name of the file you selected, and its path, in a message box, as shown in Figure 17.2.
Creating A FileThe Testing Department is on the phone again. Your new SuperDuperTextPro word-processing program is great, but shouldnt it offer users some way to save their text in a file? Hmm, you think, could be a good idea. So how do you create a file in Visual Basic? The standard way is to use the Open statement (well see another way when we work with TextStream objects later in this chapter). Heres how the Open statement works: Open pathname For mode [Access access] [lock] As [#] filenumber [Len= reclength] Here are what the various arguments mean:
If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and an error occurs. Also note that the Len clause is ignored if mode is Binary. So how do you create a file with Open? If the file specified by pathname doesnt exist, it is created when a file is opened for Append, Binary, Output, or Random modes. After youve created the file, you refer to it using the file number. Lets see an example. Here, well let users write the text in a text box, Text1, to a file on disk, file.txt, when they press a button. Because file operations are prone to error (we might run into missing diskettes, locked files, and so on), we start by checking for errors: Private Sub Command1_Click() On Error GoTo FileError ... FileError: MsgBox "File Error!" End Sub Next, we create file.txt as file #1: Private Sub Command1_Click() On Error GoTo FileError Open "c:\file.txt" For Output As #1 ... FileError: MsgBox "File Error!" End Sub Now we write the text in Text1 to the file with the Print # method: Private Sub Command1_Click() On Error GoTo FileError Open "c:\file.txt" For Output As #1 Print #1, Text1.Text ... FileError: MsgBox "File Error!" End Sub And finally we close the file: Private Sub Command1_Click() On Error GoTo FileError Open "c:\file.txt" For Output As #1 Print #1, Text1.Text Close #1 Exit Sub FileError: MsgBox "File Error!" End Sub When you add a text box, Text1, to the form, and a command button, Command1, labeled Write text to file, and run the program, you see the display much like that in Figure 17.3. When you click the command button, the new file is created and written.
Getting A Files LengthWhen you start reading files in code, it can help to know the files length (for one thing, it can tell you how many bytes to read in). There are two ways to determine file length, the FileLen and the LOF functions. The FileLen Function The FileLen function returns the length of a file (in bytes) on disk. Heres an example in which we report the size of a file, file.txt, in a message box using FileLen:
Private Sub Command1_Click()
MsgBox "The file.txt file is" & Str(FileLen("c:\file.txt")) & _
" bytes long."
End Sub
Running this code gives a result such as you see in Figure 17.4.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |