Common Dialog Box and Save- newbie question

I want to save an array of UShort integers in any chosen directory, using a Common Dialog Box. (There seem to be big differences between VB6 and VB Express.) Can anyone help And I need to load back the values, again using a Dialog Box.

Thanks,
David.


Answer this question

Common Dialog Box and Save- newbie question

  • boone10

    Thank you. That's a good start. Unfortunately I don't know enough to complete the "TODO.." part. Is it something to do with FileStream Can you give me a hint

    David.

  • markusp

  • Sachit Kachraj

    nice to hear its working

    plz mark the answer for your question as answer



  • SmokeNMirrors

    Hi,

    Notice this line of code in the sample

    Dim FileName As String = SaveFileDialog.FileName

    To get the file path from the dialog box into the code that writes and reads the file you can:

    • Add a filePath parameter to your method that writes / reads the file and pass the file path in. For example:

    WriteToFile(FileName)

    • Add a variable / field to your form that you can share between the code that launches the dialog and the code that writes / reads the file.

    To quickly read / write text to the file, you can use My.Computer.FileSystem.ReadAllText and My.Computer.FileSystem.WriteAllText.

    Best regards,



  • A_M

    Hi David,

    You can add a new MDIParent form into your project and take a look at the OpenFileDialog and SaveFileDialog sample in there. Here's a copy of the code:

    Dim OpenFileDialog As New OpenFileDialog
    OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    OpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
    If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
    Dim FileName As String = OpenFileDialog.FileName
    ' TODO: Add code here to open the file.
    End If

    Dim SaveFileDialog As New SaveFileDialog
    SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    SaveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

    If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
    Dim FileName As String = SaveFileDialog.FileName
    ' TODO: Add code here to save the current contents of the form to a file.
    End If

    Best regards,



  • felix21685

    ok, this is not for richtextbox but its for plain text. I thought it might help you get started... I would but I don't know so I won't post rich box saves... sorry!

    here is the sample code for saving message "TESTING" with a file name "testing.txt" in directory C:\
    ok

    'Open StreamWriter to Write
    Dim sw As New IO.StreamWriter("C:\")
    ' acually write the message into the file
    sw.Writeline("TESTING")
    'properly close the stream
    sw.close

    Well, I just learned this a day or two ago so I might not have the comments right, but this works (at least to save unformatted text messages)

    Hope this helps
    -Keehun Nam



  • Scott Munro

    I need a way of getting the File Path from the Dialog Box into the code that writes (and reads) the file.

    Any ideas

    David.

  • Henry Zhang

    hi,

    yes , you can use streams, you can use richtextbox.save(...) that depend on your case

    hope this helps



  • bajaexplorer

    Hi,

    After many hours of searching and Googling and reading, and incorporating the help from your reply, I've cobbled together the code below. (The File Read code is similar, using StreamReader.) It seems to do the job although there are some commands I don't understand. Maybe you can see some flaws or offer suggestions for improvements.

    Also, is there a method of saving my UShort array as binary, rather than converting it to String data and then reconverting when it loads


    Dim Save As New SaveFileDialog()
    Dim sw As System.IO.StreamWriter
    Save.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
    Save.CheckPathExists = True      'Not sure what this does.
    Save.Title = "Save Data"

    If (Save.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then

    Try
    sw = System.IO.File.CreateText(Save.FileName)

    For i = 1 To 9
    For j = 1 To 9
    sw.WriteLine(CType(U(i, j), String))
    sw.WriteLine(CType(SFlag(i, j), String))
    Next
    Next
    sw.Flush()    ' I don't know what this does.
    sw.Close()
    Catch ex As Exception    ' I don't know what this does.

    MsgBox("ERROR: " & ex.Source & " : " & ex.Message, MsgBoxStyle.OkOnly)
    End Try

    End If


    Thanks,
    David.

  • Common Dialog Box and Save- newbie question