简体   繁体   中英

Saving to text not showing anything in 'save file type'

Hi trying to save to a text file from a rich text box with following code and it is not saving and also not giving the option to save file type when the saving prompt comes up:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click
    Dim sfd As New SaveFileDialog
    sfd.ShowDialog()
    Dim myPath As String = sfd.FileName
    FileOpen(1, "mypath", OpenMode.Output)
    PrintLine(1, RichTextBox1.Text)
    FileClose(1)

To default and filter your SaveFileDialog by .txt files you would do the following prior to calling the ShowDialog method.

    sfd.Filter = "Text Files|*.txt"
    sfd.DefaultExt = ".txt"

You have the name of your variable myPath in quotations in your FileOpen method call. Remove the quotations for the saving part of this code to work. As a recommendation, you may want to check into the System.IO.File methods for this.

FileOpen(1, myPath, OpenMode.Output)

You could use the richtextbox to save the text directly

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click    
Dim sfd As New SaveFileDialog       
Try
    With sfd
        .InitialDirectory = "D:\mypath\data"  ' --> put yor full path here
        .Filter = "Text files (*.txt)|*.txt|"
        If .ShowDialog() = DialogResult.OK Then
            RichTextBox1.SaveFile(.FileName, RichTextBoxStreamType.PlainText)                
        End If
    End With
Catch ex As System.Exception
    MsgBox(ex.Message)       
End Try
End Sub

Try this ...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click    
    Dim sfd As New SaveFileDialog
    Dim FileName As String = ""
    Dim mySafeFileName As String = ""         
    Try
        With sfd
            .InitialDirectory = "D:\mypath\data"  ' --> put yor full path here
            .FileName = mySafeFileName
            .Filter = "Text files (*.txt)|*.txt|"
            If .ShowDialog() = DialogResult.OK Then
                FileName = .FileName
                FileOpen(1, FileName , OpenMode.Input)
                PrintLine(1, RichTextBox1.Text)
                FileClose(1)                    
            End If
        End With
    Catch ex As System.Exception
        MsgBox(ex.Message)       
    End Try
End Sub

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