0

您好尝试使用以下代码从富文本框中保存到文本文件,但它没有保存,也没有在出现保存提示时提供保存文件类型的选项:

    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)
4

3 回答 3

1

要默认并按 .txt 文件过滤 SaveFileDialog,您将在调用 ShowDialog 方法之前执行以下操作。

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

在 FileOpen 方法调用中,您的变量 myPath 的名称位于引号中。删除此代码的保存部分的引号以使其起作用。作为建议,您可能需要为此检查 System.IO.File 方法。

FileOpen(1, myPath, OpenMode.Output)
于 2013-05-19T23:40:30.987 回答
1

您可以使用richtextbox直接保存文本

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
于 2013-05-21T05:05:07.583 回答
0

尝试这个 ...

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
于 2013-05-20T01:15:20.277 回答