562 567




Visual Basic 6 Black Book:File Handling And File Controls
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive To access the contents, click the chapter and section titles. Visual Basic 6 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 Publication Date: 08/01/98 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




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 we’re working with binary files in Visual Basic.


Figure 17.8  Writing and reading binary data.
The code for this example is located in the filebinary folder on this book’s accompanying CD-ROM.

Accessing Any Record In A Random Access File
When you’ve 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, we’re 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.

TIP:  Besides Get and Put, you can use the Seek function to set the position at which a record will next be read or written in a file—called the read/write position—and the LOC function to determine the current read/write position.

Closing A File
How do you close a file in Visual Basic? It’s simple—you 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.


TIP:  If you want to close all files your application has open, just use the Close statement without any arguments.

Saving Files From Rich Text Boxes
You can use the SaveFile() method to save the text in a rich text box to disk, and doing that is really easy—you 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:

•  rtfRTF— 0 (the default); the rich text box control saves its contents as an RTF file.
•  rtfText— 1; the rich text box control saves its contents as a text file.

Here’s 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 that’s all it takes—now we’ve written RTF to a file. For more information on rich text boxes, see Chapter 6.


TIP:  Many word processors, like Microsoft Word, support RTF files, so you can now write text formatted files that such word processors can read in and use.

Opening Files In Rich Text Boxes
You 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:

•  rtfRTF— 0 (the default); the rich text box control saves its contents as an RTF file.
•  rtfText— 1; the rich text box control saves its contents as a text file.

Here’s 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


That’s all there is to it—it’s that easy to load in files. For more information on rich text boxes, see Chapter 6.

Saving Files From Picture Boxes
Can you save the images in picture boxes to disk files? Yes, you can, using SavePicture . Here’s how that statement works:


SavePicture picture, stringexpression


Here’s what the arguments in that statement mean:


•  picture—Picture or image control from which the graphics file is to be created
•  stringexpression—File name of the graphics file to save

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, it’s saved in BMP format. Graphics in an Image property are always saved as bitmap (BMP) files no matter what their original format.
Here’s 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 Boxes
How 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) files—just load the file’s 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






Previous
Table of Contents
Next






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.



Wyszukiwarka

Podobne podstrony:
559 562
2014kwykład3część1Redox stud dpkid(567
567 572
demo cgi 567
Antyk kultura starożytnej Grecji i Rzymu (oraz innych d~567
562 pl wyklady z dnia lutego 11
565 567
I CKN 562 97
567,8,artykul
567 francuskikrokdalej1
demo cgi 562
567 (2)
557 562

więcej podobnych podstron