- Sample Function for Writing to a File
Create a Standard EXE Project. Copy and paste the WriteFile function code block below into the Form's code module. Add an Option Explicit and a Dim Temp line of code at the very top/beginning of the code module, before the WriteFile and ReadFile functions below on this page. Write a Sub routine which responds to a Click Event (see Item #3 below) to call both of these functions, first the WriteFile, then the ReadFile function. Include a line of code in this Sub to display the contents of your testfile.txt in a textbox control. Press F5 to run your program. Click inside your textbox and see your code spring into action!
Static Function WriteFile()
'Store in Temp what you want in the file
Temp = "Sample text"
'Open file for writing with the Output Statement
Open "c:\testfile.txt" For Output As #1
'Write data in file from the Temp variable
Print #1, Temp
Close #1 'Close file
- Sample Function for Reading from a File
Open the Standard EXE Project you created in Item #1. Copy and paste the ReadFile function code block below into the Form after the accompanying WriteFile function code block. NOTE: This code will generate an error if the file does not exist where you direct it to look. Therefore, make sure your code includes a Sub to use the WriteFile function BEFORE it uses the ReadFile function. You will need to add a textbox in which to display your file's text once you've written and read it back.
Static Function ReadFile()
'Open file and read contents with the Input Statement
Open "c:\testfile.txt" For Input As #1
Do While Not EOF(1) 'Loop until end of file.
Input #1, Temp 'Store data in Temp variable
Loop 'Continue until end of file
Close #1 'Close file.
- Sample Sub Routine for a Click Event
Open the Standard EXE Project you created for Item #1. Copy and paste the Sub routine code block below directly into the Form's code module.
WriteFile 'Call the WriteFile function
ReadFile 'Call the ReadFile function
'Print the Temp variable's contents
'in the textbox when user clicks inside it
Text1 = Text1 + Temp
- Sample Sub Routine for Moving an Image Across the Screen
Create a Standard EXE Project. Add both a Timer and a Picture Box control to the Form object. Add your image to the Picture Box control using the Properties page. Copy and paste the code below into the Form's code module. Make sure the name of the Picture Box control is the same as what is in the code block below either by using the same name as in the code or by changing the name in the code. Finally, press F5 to run your program.
'Create screen coordinate variables
Dim DeltaX, DeltaY
DeltaX = 250
DeltaY = 250
'Set Timer Interval
Timer1.Interval = 250
'Show the form
'Move picture around and "bounce" off screen
Picture1.Move Picture1.Left + DeltaX, Picture1.Top + DeltaY
If Picture1.Left < ScaleLeft Then DeltaX = 250
If Picture1.Left + Picture1.Width > ScaleWidth + ScaleLeft Then
DeltaX = -250
If Picture1.Top < ScaleTop Then DeltaY = 250
If Picture1.Top + Picture1.Height > ScaleHeight + ScaleTop Then
DeltaY = -250