Visual Basic 4 in 12 Easy Lessons vel4p09.htm
Visual Basic 4 in 12 Easy Lessons vel4p09.htm
The Program's Description
Studying the File's Input
Description
Close the Application
Project 9
Extending Data and Programs
Stop and Type: 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:
Why you need a file dialog box for file selection
How to manipulate the file controls synchronize them
How to open files and associate files to a file number
Why the Write# command is the perfect command for sending data to a data file in an easy-to-read format
When to use Input# and when to use Line Input# to read data from files
The Program's Description
Figure P9.1 shows the PROJECT9.VBP opening Form window. As you can see, the project starts off 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.
Studying the File's Input
The majority of PROJECT9.VBP contains the same frame control and code found in Unit 17's file-selection frame control. The primary difference lies in PROJECT9.VBP'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 garbled 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.
Tip: This application does not take advantage of the common dialog box because of the special form and command buttons used to select and display the file. You will get a lot of good common dialog box practice if you modify the interface of this project to use a common dialog File Open box to get the filename from the user before you display the contents of the selected file. As it stands now, this project is more complicated, and therefore, provides better study, than one that would start off using a common File Open dialog box. Nevertheless, by converting the frame used by this project into a File Open dialog box, you'll gain a mastery of both frames and common dialog boxes and this project will be twice as good for you.
Input: Listing P9.1. Reading an entire file into a string variable. Description
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: NL = Chr$(13) & Chr$(10)
10:
11: If txtFiles.Text = "*.txt" Then
12: MsgBox "First, select a file", vbExclamation, "File Not Selected"
13: Exit Sub
14: End If11:
15: ' Gather the filename into a single string
16: ' Add an ending backslash if the path has none
17: If (Right$(dirList.Path, 1) <> "\") Then
18: FileSpec = dirList.Path & "\" & txtFiles.Text
19: Else
20: FileSpec = dirList.Path & txtFiles.Text
21: End If
22:
23: ' Open the file
24: Open FileSpec For Input As #1
25:
26: ' Read up to the first 30,000 characters
27: Line Input #1, ALine ' Read a line
28: Do Until (EOF(1) = True)
29: ALine = ALine + NL ' Add a newline
30: ' Make sure that the read won't overshoot string limit
31: If (count + Len(ALine)) > 30000 Then
32: Exit Do
33: End If
34:
35: FileHold = FileHold & ALine
36: count = Len(FileHold) ' Update count
37: Line Input #1, ALine ' Read a line
38: Loop
39: Close #1
40:
41: txtFile.Text = RTrim$(FileHold)
42: txtFile.Visible = True
43: End Sub
Description
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 to hold the pathname and filename.
6. 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. Define the newline character by concatenating a carriage return and line feed character together.
10. Blank lines make the program more readable.
10. Use the ASCII table when you need to concatenate control codes.
11. Make sure the default pattern is not still selected.
12. Issue an error so the user knows to select a file.
13. Terminate the procedure early so the user can select a file.
14. Terminate the If.
14. A backslash, \, always precedes a filename.
15. A remark that explains this section of the procedure.
16. The remark continues.
17. If the selected path doesn't end with a backslash, the code must add one.
18. Concatenate a backslash after the selected path and before the filename.
19. Otherwise... (the selected path already ends with a backslash).
20. Concatenate the selected path and filename.
21. Terminate the If.
22. Blank lines help separate parts of code.
23. A remark explains the purpose of this section of code.
24. Open the selected file for input. Assign the file to the filenumber 1.
25. Blank lines help separate parts of code.
26. A remark helps explain this section of the code.
26. Line Input# does not read the file's newline character combination.
27. Read the first record.
28. Loop as long as the end of file is not yet reached.
29. Append a newline character to the record just read.
30. A remark explains this section of the code.
31. String variables can contain only slightly more than 30,000 characters.
32. Terminate the procedure if the string limit was reached.
33. Terminate the If.
34. A blank line helps separate parts of the code.
35. Append the record to the string variable that holds all the records read to that point.
36. Update the count of the file length read so far.
37. Read the next line from the file.
38. Continue the loop.
39. Close all files when you're done with them.
40. A blank line helps separate parts of the code.
41. Trim off any excess spaces from the file string and display the file in the text box.
42. Make the text box visible.
43. Terminate the subroutine procedure.
Close the Application
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.
Wyszukiwarka
Podobne podstrony:
vel4p08vel4p01vel4p02vel4p06vel4p04vel4p03vel4p07vel4p05więcej podobnych podstron