This lesson taught you how to create a file-selection frame that mimics a file dialog box as well as controls the file controls from the toolbox. You learned that
your code must keep the file controls in synchronization with each other or problems will
occur, such as the directory list box that points to a drive that doesn't contain the directory listed in the directory list box.
You learned how to use
Visual Basic's file I/O commands and functions to read, append to, and write data files. Accessing data files is relatively simple.
In this lesson, you saw the following:
Figure P9.1 shows the PROJECT9.MAK opening Form window. As you can see, the project starts off extremely simply by displaying three command buttons.
Figure P9.1. Project 9's application begins with a simple
form.
Two controls, a file-selection frame and a large text box, reside on the form, but these controls are invisible to the user when the user first runs the program. When the user selects the Select a File command button, the
file-selection frame that you
learned about in Unit 17 appears. The user is expected to select a filename.
Once the user selects a filename and double-clicks the name, or presses the OK command button on the
file-selection frame, the file-selection frame goes away again (its Visible property is set to False) and the simple three-button form returns. If the
user then clicks the Display File command button, the program reads the contents of the selected file
into a single (but long) string variable. The program then displays that string variable in the text box whose Visible property is changed to True so that
the user can see and scroll through the file. A sample file being displayed in the text box is shown
in Figure P9.2.
Figure P9.2. Displaying the contents of the selected file.
The majority of PROJECT9.MAK contains the same frame control and code found in Unit 17's file-selection frame control. The primary difference lies in PROJECT9.MAK's Display File command
button's Click code shown in P9.1.
Warning: If you display a file that's not an ASCII text file, the file will appear garbaged in the text box. For example, if you select a Microsoft Excel spreadsheet, you won't see the spreadsheet inside the text box, but you will see a compressed binary representation of the spreadsheet.
The code uses the Line Input# statement to read
each record in the file that the user selected in the file-selection frame. The line-by-line description explains the code inside the cmdDisp_Click() procedure shown in Listing P9.1.
Listing P9.1. Reading an entire file
into a string variable.
1: Sub cmdDisp_Click () 2: ' Read the whole file (up to 30,000 characters) 3: ' into a single string variable. Display that 4: ' string variable in a text box. 5: Dim count As Integer ' Holds character count 6: Dim FileSpec, ALine As String 7: Dim FileHold As String ' Holds entire file 8: Dim NL As String 9: 10: NL = Chr$(13) + Chr$(10) 11: 12: ' Gather the filename into a single string 13: ' Add an ending backslash if the path has none 14: If (Right$(dirList.Path, 1) <> "\") Then 15: FileSpec = dirList.Path & "\" & txtFiles.Text 16: Else 17: FileSpec = dirList.Path & txtFiles.Text 18: End If 19: 20: ' Open the file 21: Open FileSpec For Input As #1 22: 23: ' Read up to the first 30,000 characters 24: Line Input #1, ALine ' Read a line 25: Do Until (EOF(1) = True) 26: ALine = ALine + NL ' Add a newline 27: ' Make sure that the read won't overshoot string limit 28: If (count + Len(ALine)) > 30000 Then 29: Exit Do 30: End If 31: 32: FileHold = FileHold & ALine 33: count = Len(FileHold) ' Update count 34: Line Input #1, ALine ' Read a line 35: Loop 36: Close #1 37: 38: txtFile.Text = RTrim$(FileHold) 39: txtFile.Visible = True 40: End Sub
1: The command button to display the file is named cmdDisp,
hence the event procedure subroutine name of cmdDisp_Click().
2: A remark helps explain the purpose of the procedure.
3: The remark continues.
4: The remark continues.
5: Define an integer variable that will hold the
length of the string variable as the program reads the file into the variable.
6: Define a string named FileSpec that will hold the pathname and filename. Also define a string named ALine that will hold each record from the file.
7:
Define a string variable that will hold the entire file.
8: Define a string variable that will hold the carriage return and line feed or newline character.
9: A blank line helps separate parts of the procedure.
10: Define the
newline character by concatenating a carriage return and line feed character together.
10: Use the ASCII table when you need to concatenate control codes.
11: Blank lines help separate parts of code.
12: A remark that explains this section of the procedure.
13: The remark continues.
14: If the selected path doesn't end with a backslash, the code
must add one.
14: A backslash, \, always precedes a filename.
15: Concatenate a backslash after the selected path and before
the filename.
16: Otherwise... (the selected path already ends with a backslash).
17: Concatenate the selected path and filename.
18: Terminate the If.
19: Blank lines help separate parts of code.
20: A
remark explains the purpose of this section of code.
21: Open the selected file for input. Assign the file to the filenumber 1.
22: Blank lines help separate parts of code.
23: A remark helps explain this section of the code.
24: Read the first record.
25: Loop as long as the end of file is not yet reached.
26: Append a newline character to the record just read.
26: Line Input# does not read the file's newline character combination.
27: A remark explains this section of the code.
28: String variables can contain only slightly more than 30,000 characters.
Therefore, ignore the record just read and quit reading if the record length puts the number of characters over 30,000 characters.
29: Terminate the procedure if the string limit was reached.
30: Terminate the If.
31: A blank
line helps separate parts of the code.
32: Append the record to the string variable that holds all the records read to that point.
33: Update the count of the file length read so far.
34: Read the next line from the file.
35: Continue the loop.
36: Close all files when you're done with them.
37: A blank line helps separate parts of the code.
38: Trim off any excess spaces from the file string and display the file in the text box.
39: Make the text box visible.
40: Terminate the subroutine procedure.
You can now exit the application and exit Visual Basic. The next lesson explains how to add menus to your application and manipulate a new control, the timer control.