繁体   English   中英

如何使用FolderBrowsedialog将文本框数据保存到文件

[英]How to save textbox data to file using FolderBrowsedialog

VB.NET的新手。 在下面的代码中,当我对其进行编译时,当我检查“ 选择单选按钮”并浏览该文件夹并单击GENERRATE HL7”时出现错误,错误为“ 错误:表达式是一个值,因此不能成为赋值的目标“当我检查默认单选框它就像一个魅力。 但是当我检查选择单选按钮时出现错误。 我不知道我的代码有什么问题。 您可以在以下URL中找到我的设计:[URL = http://s1065.photobucket.com/user/Izaz_Ahmed/media/Capture_zpst4jjgvxb.jpg.html]

Private Sub HL_Click(sender As Object, e As EventArgs) Handles HL.Click

        If vld(TxtProcode) = False Then
            Exit Sub
        End If

        Dim file As System.IO.StreamWriter
        Dim folderBrowser As New FolderBrowserDialog
        Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd") & DateTime.Now.ToString("HHmmss") & ".HL7"
        Dim ts As String = DateTime.Now.ToString("yyyyMMdd") & DateTime.Now.ToString("HHmmss")
        'file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)
        folderBrowser.ShowNewFolderButton = True
        If RadioBtndefault.Checked Then
            TxtDob.Format = DateTimePickerFormat.Custom
            TxtDob.CustomFormat = "yyyyMMdd"
            TxtExamtime.Format = DateTimePickerFormat.Custom
            TxtExamtime.CustomFormat = "hhMMss"
            TxtExamdate.Format = DateTimePickerFormat.Custom
            TxtExamdate.CustomFormat = "yyyyMMdd"
            file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)
            file.WriteLine("MSH|^~\&|||||" & TxtExamdate.Text & "" & TxtExamtime.Text & "||ORM^O01||P|2.3.1")
            file.WriteLine("PID|||" & TxtId.Text & "||" & TxtFamilyname.Text & "^" & TxtGivenname.Text & "||" & TxtDob.Text & "||" & TxtGender.Text & "|||" & TxtStreet.Text & " " & TxtHouse.Text & "^^" & TxtCity.Text & "^^" & TxtPostcode.Text)
            file.WriteLine("PV1||O|||||||||||||||||" & TxtId.Text & "|||||||||||||||||||||||||" & ts)
            file.WriteLine("ORC|NW|" & ts & "|||||^^^S||" & TxtExamdate.Text)
            file.WriteLine("OBR||" & ts & "^" & ts & "||" & TxtProcode.Text & "|||" & TxtExamdate.Text & "" & TxtExamtime.Text & "|" & TxtExamdate.Text & "" & TxtExamtime.Text)
            file.WriteLine()
            file.Close()
        End If

        If RadioBtnselect.Checked Then
            If folderBrowser.ShowDialog() = DialogResult.OK Then
                file.WriteLine = folderBrowser.SelectedPath
                file.WriteLine("MSH|^~\&|||||" & TxtExamdate.Text & "" & TxtExamtime.Text & "||ORM^O01||P|2.3.1")
                file.WriteLine("PID|||" & TxtId.Text & "||" & TxtFamilyname.Text & "^" & TxtGivenname.Text & "||" & TxtDob.Text & "||" & TxtGender.Text & "|||" & TxtStreet.Text & " " & TxtHouse.Text & "^^" & TxtCity.Text & "^^" & TxtPostcode.Text)
                file.WriteLine("PV1||O|||||||||||||||||" & TxtId.Text & "|||||||||||||||||||||||||" & ts)
                file.WriteLine("ORC|NW|" & ts & "|||||^^^S||" & TxtExamdate.Text)
                file.WriteLine("OBR||" & ts & "^" & ts & "||" & TxtProcode.Text & "|||" & TxtExamdate.Text & "" & TxtExamtime.Text & "|" & TxtExamdate.Text & "" & TxtExamtime.Text)
                file.WriteLine()
                file.Close()
                Dim root As Environment.SpecialFolder = folderBrowser.RootFolder
            End If
        End If
End Class

查看您的第二个条件RadioBtnselect语句。 您永远不会告诉StreamWriter路径,因此会出错。 您确实会在第一个(而不是第二个)中为其指定路径。

例如:

file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)

确切的错误消息是由此行引起的

 file.WriteLine = folderBrowser.SelectedPath

WriteLine是方法而不是属性。 语法应为WriteLine(....)。

无论如何,您的代码都会失败,因为在Select情况下使用的StreamWriter没有像在第一种情况下那样正确地初始化。
您需要像这样的东西,将SelectedPath与您的预期文件名结合在一起

If RadioBtnselect.Checked Then
    If folderBrowser.ShowDialog() = DialogResult.OK Then
        Dim destFile = Path.Combine(folderBrowser.SelectedPath,fileDateTime)
        file = My.Computer.FileSystem.OpenTextFileWriter(destFile,True)      
        file.WriteLine(.....)
        .....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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