保存到“保存文件类型”中未显示任何内容的文本

时间:2013-05-19 22:42:07

标签: vb.net visual-studio-2010

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

    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)

3 个答案:

答案 0 :(得分:1)

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

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

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

FileOpen(1, myPath, OpenMode.Output)

答案 1 :(得分: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

答案 2 :(得分: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