Frage

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!

War es hilfreich?

Lösung

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

Andere Tipps

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top