0

I'm looking to do a "Save As" dialog box to handle my saves from an application I am making in Visual Basic.

This is the code I am using and I don't understand it the code either:

Private Sub MenuExportTo_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles MenuExportTo.Click

        Dim myStream As IO.Stream  'I don't understand this line
        Dim saveFileDialog1 As New SaveFileDialog() 'I don't understand this line


        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" 
        'I understand this
        saveFileDialog1.FilterIndex = 1 ' I understand this
        saveFileDialog1.RestoreDirectory = True ' I understand this

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then 
            ' I don't understand the rest

            myStream = saveFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here. (This was what the example 
I referenced put here. the important code goes here, but the example was no help)
                myStream.Close()
            End If
        End If

Problem 1: I'm newer at this, no source gives me an in-depth enough explanation to understand what's really going with any saving method. I need a solid explanation, I'm lost here.

Problem 2: I can get the code to write to a text file at a location, but return carets are lost and it saves to the text file all on one line, using this:

My.Computer.FileSystem.WriteAllText("c:\savelocation", TextBox1.Text, False

How do I fix the retun caret problem? Also, How would I make it so the user selects the save location instead of that being part of the code?

Problem 3: How do I get the file path specified by the user, in the save as dialog, into a variable?

Any help, even just an answer to one of my questions would be appreciated! Thanks!

user2348797
  • 373
  • 4
  • 9
  • 22

2 Answers2

3

See if this example helps:

Private Sub MenuExportTo_Click(sender As System.Object, e As System.EventArgs) Handles MenuExportTo.Click
    Dim sfd As New SaveFileDialog() ' this creates an instance of the SaveFileDialog called "sfd"
    sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    sfd.FilterIndex = 1
    sfd.RestoreDirectory = True
    If sfd.ShowDialog() = DialogResult.OK Then
        Dim FileName As String = sfd.FileName ' retrieve the full path to the file selected by the user
        Dim sw As New System.IO.StreamWriter(FileName, False) ' create a StreamWriter with the FileName selected by the User
        sw.WriteLine(TextBox1.Text) ' Write the contents of TextBox to the file
        sw.Close() ' close the file
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • That worked well, and I have a better understanding of the and how it works. Thanks! But, only one problem left, it still saves to the text file all on one line. Could it be because the program I am writing is automatically entering the text in the textbox (it's not user entered). Also, it is a richtextbox. – user2348797 May 04 '13 at 03:03
  • Set WordWrap to False, and MultiLine to True. Then the file should look like what you've got in your TextBox. – Idle_Mind May 04 '13 at 03:07
  • 1
    If it's a **RichTextBox**, you can use `RichTextBox1.SaveFile(sfd.FileName)`, and skip the StreamWriter altogether. – Idle_Mind May 04 '13 at 03:09
  • The Richtextbox properties are already set to WordWrap False and Multiline True. Still writes the text file on one line. – user2348797 May 04 '13 at 03:15
  • Can you post a screenshot of your RichTextBox with its contents? – Idle_Mind May 04 '13 at 03:18
  • Here is a link directly to the image: http://imageshack.us/scaled/landing/15/11112127.png – user2348797 May 04 '13 at 03:26
  • I don't see how the resulting output file could be all on one line then. – Idle_Mind May 04 '13 at 03:41
  • Maybe because the code for the RichTextBox looks like this?" - CopyFile actived." & Environment.NewLine & "Copying every " & _ strMin & " min " & strSec Also, should we move this to chat? – user2348797 May 04 '13 at 03:49
  • RichTextBox is a bit special, it doesn't have proper line-endings. Use its SaveFile() method as recommended by @Idle or use File.WriteAllLines(), passing its Lines property. – Hans Passant May 04 '13 at 04:29
  • I still ran into problems with the other methods when using rich text box. However I did not know rich text boxes were handled differently than text boxes when writing out files. I changed the rich text box to a text box and set the multiline property to true. I feel like a doof, but using Idle_Mind's method to save, it works perfectly. Thank you both! – user2348797 May 04 '13 at 05:33
0
Private Sub MenuExportTo_Click(sender As System.Object, e As System.EventArgs) Handles MenuExportTo.Click
    Dim sfd As New SaveFileDialog() ' this creates an instance of the SaveFileDialog called "sfd"
    sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    sfd.FilterIndex = 1
    sfd.RestoreDirectory = True
    If sfd.ShowDialog() = DialogResult.OK Then
        Dim FileName As String = sfd.FileName ' retrieve the full path to the file selected by the user
        Dim sw As New System.IO.StreamWriter(FileName, False) ' create a StreamWriter with the FileName selected by the User
        sw.WriteLine(TextBox1.Text) ' Write the contents of TextBox to the file
        sw.Close() ' close the file
    End If
End Sub
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
hana
  • 1