-
Hello,
How would I set up my program so that the text in two text boxes is saveable in .txt format.
Also, how to open already saved files.
So lets say in text1, I have:
"Blah"
and in text2 I have:
"Boo!".
When I save that, I want it to be saved in a .txt file like this:
[Text1]
Blah
[Text2]
Boo!
Then when I open the file, I need VB to know which is which and put them back in the right box.
Thanks,
System
-
Private Sub Text_Save()
Dim f%
f = FreeFile
Open "c:\blah.txt" For Output As #f
Print #f, Text1
Close #f
f = FreeFile
Open "c:\boo.txt" For Output As #f
Print #f, Text2
Close #f
End Sub
Private Sub Text_Open()
Dim f%
f = FreeFile
Open "c:\blah.txt" For Input As #f
Text1 = Input(LOF(f), f)
Close #f
f = FreeFile
Open "c:\boo.txt" For Input As #f
Text2 = Input(LOF(f), f)
Close #f
End Sub
-
You could also use:
Code:
Private Sub cmdSave_Click()
Open "d:\testfile.txt" For Output As #1
Print #1, "[Text1]"
Print #1, Text1.Text
Print #1, "[Text2]"
Print #1, Text2.Text
Close #1
End Sub
Private Sub cmdOpen_Click()
Dim tString As String, tText
Open "d:\testfile.txt" For Input As #1
Do Until EOF(1)
Line Input #1, tString
If Left$(tString, 1) = "[" And Right$(tString, 1) = "]" Then
tText = Mid(tString, 2, Len(tString) - 2)
Else
Select Case tText
Case "Text1"
Text1.Text = tString
Case "Text2"
Text2.Text = tString
End Select
End If
Loop
Close #1
End Sub
-
makai:
Thanks, but that would save it in two seperate files, and I need both in one.
RealisticGraphics:
I am using that code, except for the open part, I could not get that going right. Anyway, I need to save it as "Save As..." format so that people can pick where and the name of the file. How would I change that code, so they can pick the save settings?
Thanks,
System
-
Add a common dialog control to your form (I called mine cd1) and then use the following code. BTW, what do you mean you couldn't get the Open code to work?
Code:
Private Sub cmdSave_Click()
cd1.ShowSave
Open cd1.FileName For Output As #1
Print #1, "[Text1]"
Print #1, Text1.Text
Print #1, "[Text2]"
Print #1, Text2.Text
Close #1
End Sub
Private Sub cmdOpen_Click()
Dim tString As String, tText
cd1.ShowOpen
Open cd1.FileName For Input As #1
Do Until EOF(1)
Line Input #1, tString
If Left$(tString, 1) = "[" And Right$(tString, 1) = "]" Then
tText = Mid(tString, 2, Len(tString) - 2)
Else
Select Case tText
Case "Text1"
Text1.Text = tString
Case "Text2"
Text2.Text = tString
End Select
End If
Loop
Close #1
End Sub
-
?
What is a common dialog control?
-System
-
<?>
Code:
'using common dialog
'write a text box output to a file
'called YourFile and save it as text file.
'
'open a project and go to
'Project\Components\Microsoft Common dialog Control
'click it to add it to the toolbox
'
'double click it in the toolbox to add it to the form.
Private Sub Command1_Click()
CommonDialog1.CancelError = True
CommonDialog1.Flags = cdlOFNOverwritePrompt
On Error GoTo errhandler
'Set the properties of the text control
CommonDialog1.Filter = "Text Files(*.txt)|*.txt*|HTML Files(*.htm,*.html)|*.htm*|All files(*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.FileName = "YourFile"
CommonDialog1.DefaultExt = "txt"
CommonDialog1.ShowSave
Open CommonDialog1.FileName For Output As #1
Print #1, Text1.Text
Close #1
errhandler:
Exit Sub
End Sub
-
Don't have
I don't have:
"Project\Components\Microsoft Common dialog Control"
-System
-
N/M
Never Mind, I got it set up so it will work good enough. thanks.
Thanks for you help.
(I will have more questions soo. ;^) )
-System