Page 1 of 1
VB code question

Posted:
Mon Apr 19, 2004 1:47 pm
by Dan
What is the VB6 code to print the contents of a textbox out to a *.txt (Notepad) file on the click of a cmd? This is done in both AITE and in CFSMe, some of you may know them.
Thanks, Dan G
Re: VB code question

Posted:
Mon Apr 19, 2004 11:34 pm
by gw
Dan,
Create a VB6 form and put a text box and a button on it. In design mode double click on the button and but this code in button click function that is displayed:
Open "C:\somedirectory\somefile.txt" For Output As #1
Print #1, text1.text
Close 1
gw
Re: VB code question

Posted:
Tue Apr 20, 2004 12:55 pm
by Dan
Great:- That worked. Now, how can I make it insert it somwhere in a pre-made txt template, for example:
16 May 2003
TURN WORD WRAP ON
------------------------------------The P47 Funderbolt by Dan Garner-----------------------------------------
[glb]INSERT HERE[/glb]
----------------------------------------------------Installation--------------------------------------------------------------
Thanks, Dan
Re: VB code question

Posted:
Tue Apr 20, 2004 10:51 pm
by gw
Dan,
There are lots of ways to do it but here's one:
Your template is going to live in some file so you can keep it between executions. That means that at some point you have to read it in, write the front of it to your new output file, write your text and then write the back of the template. For example:
'define some variables we'll need
dim fno1 as integer
dim fno2 as integer
dim inarea as string
'open the template file for reading
fno1 = FreeFile
Open "C:\somedirectory\template.txt" For Input As #fno1
'open the output file for writing
fno2 = FreeFile
Open "C:\somedirectory\somefile.txt" For Output As #fno2
'copy front of template to output
line input #fno1, inarea
do while inarea <> "insert here"
print #fno2, inarea
line input #fno1, inarea
loop
'write text box contents to output file
print #fno2, text1.text
'copy back of template to output
do while not eof(fno2)
line input #fno1, inarea
print #fno2, inarea
loop
'close both files
close fno1
close fno2
"line input" reads an entire line of text into the specified string variable without regard to its contents.
gw
Re: VB code question

Posted:
Wed Apr 21, 2004 7:44 am
by Dan
When I try to print it stops on this line: Open "C:\Aafamily\Daniel\CFS World\ReadMe!.txt" For Output As #fno2 , with a Run time 55, file already open.
If I quote that out, it says RunTiem 52, bad file name or number, the line being Print #fno2, inarea.
If i quote that I get the same error on the same line further down. ' again and it goes to Do While Not EOF(fno2). Again and it does loop without do?
What am I doing wrong?
Dan
Re: VB code question

Posted:
Wed Apr 21, 2004 8:00 pm
by gw
Dan,
You've probably got a typo somewhere. Do you know how to use f8 and f9 to single step a program and set breakpoints? Put the cursor on the first line of executable code and hit f9. The line will be highlighted in red. Then hit f5 to run the program. When it gets to that line of code VB will stop and let you look at the code. hit f8 and it will execute one line of code and stop. If you let the cursor hover over a variable name VB will tell you what its current is. fno1 and fno2 should have consecutive values. If fno1 is 3 then fno2 should be 4. In any case they shouldn't be the same.
You got the errors on the prints because you had commented out the open. Print won't work unless you've opened the file.
If all else fails you can get rid of fno1 and fno2 and use explicit integers as in the first piece of code at the top.
And maybe the typo is mine. I'm at home and don't have my vb6 compiler to check my work before I post it.
gw
Re: VB code question

Posted:
Thu Apr 22, 2004 2:35 pm
by Dan
It still doesn't work and my VB doesn't seem to show me what the variable is. Here is my current code:
Option Explicit
Private Sub cmdPrint_Click()
'define some variables we'll need
Dim fno1 As Integer
Dim fno2 As Integer
Dim inarea As String
'open the template file for reading
fno1 = FreeFile
Open "C:\Aafamily\Daniel\CFS World\ReadMe!.txt" For Input As #fno1
'open the output file for writing
fno2 = FreeFile
Open "C:\Aafamily\Daniel\CFS World\ReadMe!.txt" For Output As #fno2
'copy front of template to output
Line Input #fno1, inarea
Do While inarea <> "insert here"
Print #fno2, inarea
Line Input #fno1, inarea
Loop
'write text box contents to output file
Print #fno2, text1.Text
'copy back of template to output
Do While Not EOF(fno2)
Line Input #fno1, inarea
Print #fno2, inarea
Loop
'close both files
Close fno1
Close fno2
End Sub
Thanks, Dan
Re: VB code question

Posted:
Thu Apr 22, 2004 10:34 pm
by gw
Aha!!!
The file you're opening for output is the same name as the file you're opening for input. So, yes, when you get to the second open VB says the file is already open--you opened it in the first open.
Tell me a bit more about what you're trying to do exactly.
gw
Re: VB code question

Posted:
Fri Apr 23, 2004 1:26 pm
by Dan
I am trying to make a readme builder for uploads etc... I want to insert parts into a readme like this one:
16 May 2003
TURN WORD WRAP ON
------------------------------------The P47 Funderbolt by Dan Garner-----------------------------------------
This aircraft was worked on entirely from stock by Dan Garner. It has been re-painted to a semi-real Thunderbolt from RAF Duxford. The *Air file (Flight Characteristics) has been drastically modified to make it a fully aerobatic aircraft. The damage profile (*dp) has been altered as well to take the P51 Mustang profile.
----------------------------------------------------Installation--------------------------------------------------------------
For those amongst you who don't know (!), here are some brief installation instructions:
Having unzipped this file to a suitable directory, for example 'C:/Myfiles', (NOT CFS), make yourself familiar with the CFS directories. It should look something like: 'C:/MicrosoftGames/CombatFlightSimulator/Aircaft' If it does not compare exactly, don't worry! Just copy the 'Funderbolt' folder in 'C:/Myfiles' to the 'Aircraft' folder in CFS. Fire up the simulator, and on the aircraft menu it will show up as 'P47 Funderbolt'.
This aircraft has been designed especially for the original Combat Flight Simulator (Europe Series) (CFS1). It has NOT been tested on any other systems.
PLEASE NOTE: With some different size monitors the panel 'windows' may apear blacked out. Just exit the CFS and open the file called 'Panel_Background_1024' in 'CFS/Aircraft/Funderbolt/Panel' and paint the top left few pixels TRUE black. Save, close the file and start the sim. You should now see out of the windows.
------------------------------------------------------Declarations----------------------------------------------------------
I would like to thank a few people for their help and contribution towards my project:-
William Roth - For his excellent freeware 'AirEd'
Simviation Forum's - For all of the members's help for me when I had problems
Simviations 'Lair' - The 'Lair' helped me a lot through many little problems
Above all, thank you to Simviation, without whom I would never have done this!
I TAKE ABSOLUTEY NO RESPONSIBILITY FOR ANY DAMAGE TO FILES OR OTHER PROBLEMS CAUSED BY THESE FILES. Any other designers may take this design on provided that:
NO profit is taken - this is freeware, it MUST stay that way.
Credit is given to me where required.
-------------------------------------------------Contact Me----------------------------------------------------------------
Please let me know about any constructive thoughts you have, by sending me an Instant Message on the Simviation forum. Please don't send me hate mail. My username is: Dan
Thanks v much,
Dan
Re: VB code question

Posted:
Fri Apr 23, 2004 10:58 pm
by gw
Dan,
If that's the case then I don't think you want to overwrite your input file otherwise your program will only run once. How about naming it something like readme-template.txt.
Your output file becomes the readme.txt that gets distributed so it's the one that gets written to.
So if you read from readme-template.txt and write to readme.txt VB6 won't have a problem with your open statements.
gw
Re: VB code question

Posted:
Sat Apr 24, 2004 7:32 am
by Dan
Goody... Why didn't I think of that ???
dan
Re: VB code question

Posted:
Sat Apr 24, 2004 6:53 pm
by gw
Learning a programming language like vb6 is the first part of programming. It's like learning how to use a set of caprenter's tools. After you know how to use the tools then you learn how to build cabinets, houses, etc. There are tons of "why didn't I think of that" ways of doing things that you'll pick up and you'll think of a lot of them yourself.
gw