Creating a New Text File on Disk
To create a new text file on disk by using Visual Basic, you can use many of the functions and keywords used in the last example. Creating new files on disk and saving data to them is useful if you plan to generate custom reports or logs, save important calculations or values, or create a special-purpose word processor or text editor. Here's an overview of the steps you'll need to follow in the program:
Get input from the user or perform mathematical calculations, or do both.
Assign the results of your processing to one or more variables. For example, you could assign the contents of a text box to a string variable named InputForFile.
Prompt the user for a path by using a SaveFileDialog control. You use the ShowDialog method to display the dialog box.
Use the path received in the dialog box to open the file for output.
Use the PrintLine function to save one or more values to the open file.
Close the file when you're finished by using the FileClose function.
The following exercise demonstrates how you can use TextBox and SaveFileDialog controls to create a simple note-taking utility. The program uses the FileOpen function to open a file, the PrintLine function to store string data in it, and the FileClose function to close the file. You can use this program to take notes at home or at work and then to stamp them with the current date and time.
Run the Quick Note program
Click the Close Solution command on the File menu.
Open the Quick Note project in the c:\vb05sbs\chap13\quick note folder.
The project opens in the IDE.
If the project's form isn't visible, display it now.
The Quick Note form appears, as shown in the following illustration. It looks similar to the Text Browser form. However, I replaced the OpenFileDialog control with the SaveFileDialog control on the form. The File menu contains the Save As, Insert Date, and Exit commands.
I set the following properties in the project:
Object | Property | Setting |
txtNote | Multiline Name ScrollBars | True txtNote Vertical |
lblNote | Text | “Type your note and then save it to disk.” |
Form1 | Text | “Quick Note” |
Click the Start Debugging button.
Type the following text, or some text of your own, in the text box:
How to Detect Counterfeit Coins
1) Drop coins on a hard surface. Genuine coins have a bell-like ring; most counterfeit coins sound dull.
2) Feel all coins. Most counterfeit coins feel greasy.
3) Cut edges of questionable coins. Genuine coins are not easily cut.
When you're finished, your screen looks similar to this:
TIP
To paste text from the Windows Clipboard into the text box, press Ctrl+V or Shift+Insert. To copy text from the text box to the Windows Clipboard, select the text, and then press Ctrl+C.
Now try using the commands on the File menu.
On the File menu, click the Insert Date command.
The current date and time appears as the first line in the text box, as shown here:
The Insert Date command provides a handy way to include the current time stamp in a file, which is useful if you're creating a diary or a logbook.
On the File menu, click the Save As command.
The program displays a Save As dialog box with all the expected features. The default file type is set to .txt. Your screen looks like this:
In the Save As dialog box, open the c:\vb05sbs\chap13\quick note folder if it isn't already open. Then type Badcoins.txt in the File Name text box, and click Save.
The text of your document is saved in the new Badcoins.txt text file.
On the File menu, click the Exit command.
The program stops, and the development environment returns.
Now you'll take a look at the event procedures in the program.
Examine the Quick Note program code
On the Quick Note form File menu, double-click the Insert Date command.
The InsertDateToolStripMenuItem_Click event procedure appears in the Code Editor. You see the following program code:
txtNote.Text = My.Computer.Clock.LocalTime & vbCrLf & txtNote.Text txtNote.Select(1, 0) 'remove selection
This event procedure adds the current date and time to the text box by linking together, or concatenating, the current date (generated by the My object and the LocalTime method), a carriage return (added by the vbCrLf constant), and the Text property. You could use a similar technique to add just the current date (by using DateString) or any other information to the text in the text box.
Take a moment to see how the concatenation statements work, and then examine the SaveAsToolStripMenuItem_Click event procedure in the Code Editor.
You see the following program code:
SaveFileDialog1.Filter = "Text files (*.txt)|*.txt" SaveFileDialog1.ShowDialog() If SaveFileDialog1.FileName <> "" Then FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output) PrintLine(1, txtNote.Text) 'copy text to disk FileClose(1) End If
This block of statements uses a save file dialog object to display a Save As dialog box, verifies whether the user selected a file, opens the file for output as file number 1, writes the value in the txtNote.Text property to disk by using the PrintLine function, and then closes the text file. Note especially the statement
PrintLine(1, txtNote.Text) 'copy text to disk
which assigns the entire contents of the text box to the open file. PrintLine is similar to the older Visual Basic Print and Print # statements; it directs output to the specified file rather than to the screen or the printer. The important point to note here is that the entire file is stored in the txtNote.Text property.
Review the FileOpen, PrintLine, and FileClose functions, and then close the program by using the Close Solution command on the File menu.
You're finished with the Quick Note program.