简体   繁体   中英

How to put file save location into a variable in VB with richtextbox

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM