Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Registry & File Input / Output

Post new topic   Reply to topic
Help with saving text from a file . . .
View previous topic :: View next topic  
Author Message
greg_schmit
Newbie


Joined: 11 Apr 2005
Posts: 2

PostPosted: Apr 11th, 2005 06:07 PM    Post subject: Help with saving text from a file . . . Reply with quote

I have been trying so save all the text in a text file as a string, and then, open it in a text box for editing, and then after the person exits, it saves all that text in the text file. At first I tryed to open the file as a string (HAHA), but that didn't work. If anyone could give me the syntax to do this . .that would be great =D.
_________________
If losers never quit, and quitters can't win . . .then what moron came up with "quit while you're ahead"??
-
If light travels faster than sound, then how come some people appear bright until you hear them speak??
-
If you eat pasta, and anti-pasto, would you still be hungry??
-
What happens when an unstoppable and unbreakable force, meets an unbreakable and unmoveble object?
Back to top
View user's profile Send private message Send e-mail
Jaguar
Newbie


Joined: 09 Mar 2005
Posts: 8
Location: Central Texas

PostPosted: Apr 18th, 2005 03:47 PM    Post subject: Reply with quote

Here' some simple code. (Make sure you set your textbox to Multiline=True and ScrollBars=Both)
Code:

Option Explicit
Dim sPath As String

Private Sub cmdOpen_Click()
Dim iFile As Integer
Dim sTemp As String
'Handle errors
On Error GoTo AnError
'Get path
sPath = InputBox("Enter a valid text(*.txt) filename including path.")
'Clear textbox
Text1.Text = ""
'Get a free file
iFile = FreeFile
'Open file
Open sPath For Input As #iFile
'Loop to get whole text
Do While Not EOF(iFile)
    Input #iFile, sTemp
    If Text1.Text = "" Then
        'Don't add break
        Text1.Text = Text1.Text & sTemp
    Else
        'Add break
        Text1.Text = Text1.Text & vbCrLf & sTemp
    End If
Loop
'Close file
Close #iFile
'Done
Exit Sub
'Error Handler
AnError:
MsgBox Error$, vbCritical
End Sub

Private Sub cmdSave_Click()
Dim iFile As Integer
Dim sTemp As String
'Handle errors
On Error GoTo AnError
'Get a free file
iFile = FreeFile
'Make new file
Open sPath For Output As #iFile
'Add text
Print #iFile, Text1.Text
'Close file
Close #iFile
'Done
MsgBox "Saved"
Exit Sub
'Error Handler
AnError:
MsgBox Error$, vbCritical
End Sub

_________________
VB = "Fun"
Back to top
View user's profile Send private message
RoofRabbit
Regular


Joined: 06 Jul 2005
Posts: 95
Location: Lenoir, NC USA

PostPosted: Jul 6th, 2005 11:04 PM    Post subject: Reply with quote

I have an example program (project) on my website to do exactly that, it only has one form with 3 buttons and a textbox and the common dialog control. The entire vb code is:
Code:



Private Sub CmdExit_Click()
    Unload Me
End Sub

Private Sub CmdOpen_Click()
    Dim ch As Integer
    Dim ts As String
    On Error GoTo endcmdopen
    CD.CancelError = True
    CD.InitDir = App.Path
    CD.Filter = "*.txt|*.txt"
    CD.ShowOpen
    Text1.Text = ""
    ch = FreeFile
    Open CD.FileName For Input As ch
    Do While Not EOF(ch)
        Line Input #ch, ts
        Text1.Text = Text1.Text & ts & vbCrLf
    Loop
    Close #ch
endcmdopen:
End Sub

Private Sub CmdSave_Click()
    Dim ch As Integer
    Dim ts As String
    On Error GoTo endcmdsave
    CD.CancelError = True
    CD.InitDir = App.Path
    CD.Filter = "*.txt|*.txt"
    CD.ShowSave
    ch = FreeFile
    Open CD.FileName For Binary As ch
        Put #ch, , Text1.Text
    Close #ch
endcmdsave:
End Sub

You can download the source from my site complete with the form.
_________________
Website - [link]
Back to top
View user's profile Send private message Visit poster's website ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Registry & File Input / Output All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic