Results 1 to 9 of 9

Thread: Saving And Opening

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    Cool

    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

  2. #2
    Lively Member
    Join Date
    Aug 2000
    Posts
    125
    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

  3. #3
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    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
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    Cool

    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

  5. #5
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655
    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
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    Question ?

    What is a common dialog control?

    -System

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    Don't have

    I don't have:
    "Project\Components\Microsoft Common dialog Control"

    -System

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    26

    Cool 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width