Question

I am new to Visual Basic, so sorry if I this doesn't make amazing sense, but if you ask questions I should be able to clarify what I mean.

I am saving a file from a richtextbox using this code:

    Dim saveFile As New SaveFileDialog()

    saveFile.DefaultExt = "*.rtf"
    saveFile.Filter = "RTF Files|*.rtf"

    If (saveFile.ShowDialog() = System.Windows.Forms.DialogResult.OK) And (saveFile.FileName.Length) > 0 Then
        rtb_Output.SaveFile(saveFile.FileName, _
        RichTextBoxStreamType.PlainText)

    End If

I have to have the user put in the file path and name, hence the Save Dialog box. I was wondering if there is any way for me to store the file path informatin in a variable to be used later?

Était-ce utile?

La solution

If your later means in other parts of your code outside this method, then you have to declare a global variable at the class level and assign it to the value of FileName

Public Class Form1

    Dim savedFile As String


    Public Sub Form_Load(.....)
    End Sub

    Public Sub ButtonUploadFile_Click(....)

        if savedFile.Length > 0 Then
            .....
        End If
    End Sub

    Public Sub ButtonSave_Click(......)

         Dim saveFile As New SaveFileDialog()

         saveFile.DefaultExt = "*.rtf"
         saveFile.Filter = "RTF Files|*.rtf"

         If (saveFile.ShowDialog() = DialogResult.OK) AndAlso _
            (saveFile.FileName.Length) > 0 Then
             savedFile = saveFile.FileName
             rtb_Output.SaveFile(savedFile, RichTextBoxStreamType.PlainText)
         End If
    End Sub

End Class

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top