|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Next, we use Get to read in the number and store it in a new variable, varInput: Private Sub Command2_Click() Dim varInput As Double On Error GoTo FileError Open "c:\binary.dat" For Binary As #1 Get #1, , varInput ... FileError: MsgBox "File Error!" End Sub Finally, we display the newly read-in variable in a text box, Text2, and close the file: Private Sub Command2_Click() Dim varInput As Double On Error GoTo FileError Open "c:\binary.dat" For Binary As #1 Get #1, , varInput Text2.Text = Str(varInput) Close #1 Exit Sub FileError: MsgBox "File Error!" End Sub The result appears in Figure 17.8, where we write the number 3.1415 out to disk in the file binary.dat and then read it in again. Now were working with binary files in Visual Basic.
The code for this example is located in the filebinary folder on this books accompanying CD-ROM. Accessing Any Record In A Random Access FileWhen youve set up a file to hold records (by creating it in Random mode with the Open statement and passing the length of the records you want to open), you can use Get to access any record in the file by record number: Get #1, recordnumber, variablename In this case, were reading record number recordnumber from file 1 and placing the data read into a variable named variablename . In the same way, you can write any record with Put: Put #1, recordnumber, variablename Using Get and Put in this way, you can read and write any record in the file.
Closing A FileHow do you close a file in Visual Basic? Its simpleyou just use the Close statement: 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 Closing a file writes all its data out to disk.
Saving Files From Rich Text BoxesYou can use the SaveFile() method to save the text in a rich text box to disk, and doing that is really easyyou just use SaveFile() this way: RichTextBox.SaveFile( pathname, [filetype]) You can save text as plain or RTF text; the settings for filetype are as follows:
Heres an example where we display some text in a rich text box: Private Sub Form_Load() RichTextBox1.Text = "This is the text in the file." End Sub Next, we save that text to a file this way:
Private Sub Command1_Click()
RichTextBox1.SaveFile ("c:\data.txt")
End Sub
And thats all it takesnow weve written RTF to a file. For more information on rich text boxes, see Chapter 6.
Opening Files In Rich Text BoxesYou can write files to disk from a rich text box with SaveFile() ; how can you read files back in? You use LoadFile() . Like SaveFile(), LoadFile() is very easy to use: RichTextBox.LoadFile pathname, [filetype] And you can load in plain text or RTF text files; the settings for filetype are as follows:
Heres an example where we load in the file we wrote in the last topic on saving files, data.txt: Private Sub Command1_Click() RichTextBox1.LoadFile "c:\data.txt" End Sub Thats all there is to itits that easy to load in files. For more information on rich text boxes, see Chapter 6. Saving Files From Picture BoxesCan you save the images in picture boxes to disk files? Yes, you can, using SavePicture . Heres how that statement works: SavePicture picture, stringexpression Heres what the arguments in that statement mean:
Note that SavePicture only saves images in BMP, WMF, and ICO formats (depending on the file type the image came from originally); if the image came from a GIF or JPEG file, its saved in BMP format. Graphics in an Image property are always saved as bitmap (BMP) files no matter what their original format. Heres an example where we save the image from Picture1 to a file, \image.bmp, when the user clicks a button: Private Sub Command1_Click() SavePicture Picture1.Picture, "c:\image.bmp" End Sub Opening Files In Picture BoxesHow do you open image files in a picture box? You use the Picture property. A picture box is very versatile and can display images from bitmap (.bmp), icon (.ico), metafile (.wmf), JPEG (.jpg), or GIF (.gif) filesjust load the files name into the Picture property. You can use LoadPicture() to load in a picture like this, where we load in an image when the user clicks a command button:
Private Sub Command1_Click()
Picture1.Picture = LoadPicture("c:\vbbb\picturesandimages\image.bmp")
End Sub
|
|
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. |