Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How do I save to file?

How do I save to file?

Scheduled Pinned Locked Moved Visual Basic
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrGee
    wrote on last edited by
    #1

    I have a project that has a RichTextBox that a user can enter data. The text box is called txtOut. I have a CommonDialog control and a command control called cmdSave with this code attached: Private Sub cmdSave_Click() Dim sFileName As String sFileName = txtOut.Text '& theExtension ' Set CancelError is True CommonDialog1.CancelError = True On Error GoTo ErrHandler ' Set flags CommonDialog1.Flags = cdlOFNHideReadOnly ' Set filters CommonDialog1.Filter = "All Files (*.*)|*.*|Hex Files" & _ "(*.hex)|*.hex|Assembly Files (*.asm)|*.asm" ' Specify default filter CommonDialog1.FilterIndex = 2 ' Display the Open dialog box CommonDialog1.ShowSave ' Display name of selected file MsgBox CommonDialog1.FileName Exit Sub ErrHandler: 'User pressed the Cancel button Exit Sub End Sub Everything works fine except after I name the file to be saved and hit the save button in the dialog window, it doesn't create the save file. I'm guessing it doesn't see the data in the text box. Can someone give me some help please? Thanks!:)

    M 1 Reply Last reply
    0
    • M MrGee

      I have a project that has a RichTextBox that a user can enter data. The text box is called txtOut. I have a CommonDialog control and a command control called cmdSave with this code attached: Private Sub cmdSave_Click() Dim sFileName As String sFileName = txtOut.Text '& theExtension ' Set CancelError is True CommonDialog1.CancelError = True On Error GoTo ErrHandler ' Set flags CommonDialog1.Flags = cdlOFNHideReadOnly ' Set filters CommonDialog1.Filter = "All Files (*.*)|*.*|Hex Files" & _ "(*.hex)|*.hex|Assembly Files (*.asm)|*.asm" ' Specify default filter CommonDialog1.FilterIndex = 2 ' Display the Open dialog box CommonDialog1.ShowSave ' Display name of selected file MsgBox CommonDialog1.FileName Exit Sub ErrHandler: 'User pressed the Cancel button Exit Sub End Sub Everything works fine except after I name the file to be saved and hit the save button in the dialog window, it doesn't create the save file. I'm guessing it doesn't see the data in the text box. Can someone give me some help please? Thanks!:)

      M Offline
      M Offline
      Martin Cross
      wrote on last edited by
      #2

      It's because the CommonDialogs only display dialogs to gather information from the user. So the Open, Save and Print dialogs don't actually do the opening, saving or printing for you. You have to implement that yourself. So in your case you have to check that the user has succesfully OKed a filename and then use something like a StreamWriter to save your file.

      M 1 Reply Last reply
      0
      • M Martin Cross

        It's because the CommonDialogs only display dialogs to gather information from the user. So the Open, Save and Print dialogs don't actually do the opening, saving or printing for you. You have to implement that yourself. So in your case you have to check that the user has succesfully OKed a filename and then use something like a StreamWriter to save your file.

        M Offline
        M Offline
        MrGee
        wrote on last edited by
        #3

        I am not an experienced coder, is there sample code or information available on how to do that? Thanks!:)

        M P 2 Replies Last reply
        0
        • M MrGee

          I am not an experienced coder, is there sample code or information available on how to do that? Thanks!:)

          M Offline
          M Offline
          Martin Cross
          wrote on last edited by
          #4

          Here's something I've done in the past, you should be able to pull it apart for your own stuff: Private Sub SaveINIFile() Dim strCurrentSection As String Dim ptrRecord As INILayout If strFileName = "" Then Return ptrFileWriter = New System.IO.StreamWriter(strFileName) For Each strCurrentSection In colINISections ptrFileWriter.WriteLine("[" & strCurrentSection & "]") For Each ptrRecord In colINIEntries If ptrRecord.strSection = strCurrentSection Then ptrFileWriter.WriteLine(ptrRecord.strHeader & "=" & ptrRecord.strValue) End If Next Next ptrFileWriter.Close() End Sub

          1 Reply Last reply
          0
          • M MrGee

            I am not an experienced coder, is there sample code or information available on how to do that? Thanks!:)

            P Offline
            P Offline
            phlipping
            wrote on last edited by
            #5

            It's quite simple

            Open CommonDialog1.FileName For Output As #1

            Print #1, "Hello!"
            Print #1, "Green is Purple"
            Print #1, "--------"
            Print #1, Text1.Text

            Close #1

            HTH ===== Phlip Always proofread carefully to see if you any words out

            M 1 Reply Last reply
            0
            • P phlipping

              It's quite simple

              Open CommonDialog1.FileName For Output As #1

              Print #1, "Hello!"
              Print #1, "Green is Purple"
              Print #1, "--------"
              Print #1, Text1.Text

              Close #1

              HTH ===== Phlip Always proofread carefully to see if you any words out

              M Offline
              M Offline
              MrGee
              wrote on last edited by
              #6

              Thanks to everyone for their help! This is what I ended up using: Private Sub cmdSave_Click() 'When users clicks Save 'note: the entire file is stored in a string CommonDialog1.Filter = "Hex files (*.HEX)|*.HEX" CommonDialog1.ShowSave 'display Save dialog If CommonDialog1.FileName <> "" Then Open CommonDialog1.FileName For Output As #1 Print #1, txtOut.Text 'save string to file Close #1 'close file End If End Sub

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups