简体   繁体   English

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

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

Am new to VB.NET. VB.NET的新手。 In the Below code when i compile it, Am getting an error when i check the Select Radiobutton and browse the folder and clikc the GENERATE HL7 Message am getting an error as " Error: Expression is a value and therefore cannot be the target of an assignment. " when i check the Default Radiobutton it works like a charm. 在下面的代码中,当我对其进行编译时,当我检查“ 选择单选按钮”并浏览该文件夹并单击GENERRATE HL7”时出现错误,错误为“ 错误:表达式是一个值,因此不能成为赋值的目标“当我检查默认单选框它就像一个魅力。 But when i check the Select Radiobutton am getting the error. 但是当我检查选择单选按钮时出现错误。 I don't know whats wrong in my code. 我不知道我的代码有什么问题。 you can find my design in the following URL: [URL= http://s1065.photobucket.com/user/Izaz_Ahmed/media/Capture_zpst4jjgvxb.jpg.html] 您可以在以下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

Look at your second condition, RadioBtnselect statement. 查看您的第二个条件RadioBtnselect语句。 You never tell the StreamWriter the path, hence your error. 您永远不会告诉StreamWriter路径,因此会出错。 You do give it a path in your first if but not in the second one. 您确实会在第一个(而不是第二个)中为其指定路径。

For example this: 例如:

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

The exact error message is caused by this line 确切的错误消息是由此行引起的

 file.WriteLine = folderBrowser.SelectedPath

WriteLine is a method not a property. WriteLine是方法而不是属性。 The syntax shoud be WriteLine(....). 语法应为WriteLine(....)。

In any case your code will fail because the StreamWriter used in the Select case is not initalized correctly as you do in the first case. 无论如何,您的代码都会失败,因为在Select情况下使用的StreamWriter没有像在第一种情况下那样正确地初始化。
You need something like this that combine the SelectedPath with your intended file name 您需要像这样的东西,将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