Content
You may want to read some data from an input file and write results into another output file. In these cases, it is useful to use a plain text file (*.txt) which includes no information other than plain text. Files for other software (e.g., MS Word) contain complicated data that are used to format the information in the file.
Develop the "form" shown in the figure above that we will use in the project. The form should contain a label, a text box, two command buttons, and a picture box with the following properties:
| Object | Property | Setting |
| Form | Caption Name |
Input Output frmIO |
| Command Button | Caption Name |
Press to process list cmdProcess |
| Picture Box | Name | picOutput |
Your form should look like this:

Next, lets add some code to the cmdProcess button. First, double click on the cmdProcess button and type in the following code (note that it is slightly simpler than the code on page 222 in the text):

Let's talk about some of the code here:
Open "C:\temp\CE311K\InputOutput\phone.txt" For Input As #1
Open is the command to open a file,
"C:\temp\CE311K\InputOutput\phone.txt" is
the directory path and the name of the file to be opened. The directory
path presents a special problem because if you move the project to another
directory, then the program won't be able to find the input file. We will
discuss ways to solve this problem later in the lab. For now, be sure that
the path is correct for the location of your file "phone.txt".
For Input specifies the type of the file to open (input file),
#1 is the number to refer the file
in the rest of the program (1 is used here, but you can use any other integers
between 1 and 511),
Do While Not (EOF(1))
This is a do-while loop where the termination is when "Not
(EOF(1))" becomes "true". That is, when the EOF (End of File) character
has not been found in file #1, keep executing the statements in the loop.
Input #1, lastName, firstName, phoneNum
Input is the command to read data from
the file referred as #1 and assign it to the list of variables:
lastName, firstName, phoneNum

The computed result
Next, suppose we want to write the results to another file, an output file? We need to add some more lines to our code. Double click on the cmdProcess button and type in the following code (note that it is slightly simpler than the code on page 222 in the text):

Code to Input from a File and Output into another File
Let's talk about some of the code here:
Open "C:\temp\CE311K\InputOutput\fonelist.txt" For Output As #3
Open is the command to open a file,
"C:\temp\CE311K\InputOutput\fonelist.txt" is
the directory path and the name of the file to be created and opened.
For Output specifies the type of the file to open (output file),
#3 is the number to refer the file
in the rest of the program (3 is used here, but you can use any other integers
between 1 and 511),
Print #3, lastName, firstName, phoneNum
Print is the command to
write data to the file referred as #3 and the values of the list of
variables, lastName, firstName, phoneNum, is the information that is
printed in the file.
If you open the file (fonelist.txt), you should see the result as follows.

The Result in the Output File
VB has 5 standard dialog boxes that allow you to perform various useful function. These dialog objects are
| Dialog Box | Purpose | Method |
| Open | Get the drive, directory name, and filename of an existing file | ShowOpen |
| Save as | Get the drive, directory name, and filename for a new file | ShowSave |
| Let the user set printing properties | ShowPrinter | |
| Font | Let the user choose a new font type and style | ShowFont |
| Color | Let the user select a color from a palette | ShowColor |
Add the CommonDialog control to your toolbox by selecting Project/Components, select Microsoft Common Dialog Control 6.0, and then click OK. Now you will see the CommonDialog control on the toolbox in the lower right-hand corner.


To display a dialog box in a program, you need to call the dialog object. Then, when the user makes his/her choices in the dialog box, the procedure will process the choice using the program code.

Let's discuss the code a bit here. First, we added back in the picOutput.Print lines because it is very disconcerting for the user to press the button and not receive any visible feedback from the program.
CommonDialog1.ShowOpen
Open CommonDialog1.FileName For Input As #1
The first line causes the Common Dialog Control: Open dialog box to be displayed. The method for displaying the Open Dialog Box is ".ShowOpen" (see the previous table for the methods that display other dialog boxes). The user is then able to select the file to open and the program stores the name and directory path.
The second line uses the FileName method to open the file selected in the dialog box by the user. When the user selects a drive, directory, and filename and then clicks OK, the complete pathname is passed to your program through the CommonDialog1.FileName property.
Next we need to get the output filename and location from the user:
CommonDialog1.ShowSave
Open CommonDialog1.FileName For Output As #3
The first line causes the Common Dialog Control: Save as dialog box to be displayed. The method for displaying the Save as Dialog Box is ".ShowSave" (see the previous table for the methods that display other dialog boxes). The user is then able to type in the name of the file to save and the program creates the file, stores the name and directory path. In case the file already exists and its name is displayed in the dialog box, the user can simply select that name and the old file is overwritten by the new one.
The second line uses the FileName method to open the file selected in the dialog box by the user. When the user selects a drive, directory, and filename and then clicks OK, the complete pathname is passed to your program through the CommonDialog1.FileName property.
Now run your project and see how the new items work. When the user presses the button, the Open Dialog Box will appear and the input file can be selected.

Next the Save As Dialog box is shown and the user can decide on an output file name. Be sure to ad the extension ".txt" to the file name.

Write a VB code with graphic user interface to solve Chapter 5, Programming Project 1, page 125. Use the Open Dialog Box and the Save As dialog Box in your program to select the input and output files. Print all results to a picture box on the screen and to the output file. Use the following data for your input file and calculations. Find the average annual temperature in Austin.
Monthly Average High Temperature in Austin, TX.
|
Month |
Average High (oF) |
|
January |
58 |
|
February |
63 |
|
March |
71 |
|
April |
79 |
|
May |
84 |
|
June |
91 |
|
July |
95 |
|
August |
95 |
|
September |
90 |
|
October |
82 |
|
November |
71 |
|
December |
62 |
Turn in:
A copy of the VB code for your program.
A copy of the source code, the input file and the output file for your program.
A screencapture of the output from your program (the picture box display).