2015-06-07 183 views
0

VB.NET新增功能。在下面的代碼當我編譯它時,我得到一個錯誤,當我檢查選擇 Radiobutton並瀏覽文件夾和clikc GENERATE HL7消息我得到一個錯誤爲「錯誤:表達式是一個值,因此不能「當我檢查默認 Radiobutton它的作品就像一個魅力。但是,當我檢查選擇 Radiobutton我得到的錯誤。我不知道我的代碼有什麼問題。你可以找到我在下面的URL設計:[URL = http://s1065.photobucket.com/user/Izaz_Ahmed/media/Capture_zpst4jjgvxb.jpg.html]如何使用FolderBrowsedialog將文本框數據保存到文件

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 

回答

0

確切的錯誤消息是由該行

file.WriteLine = folderBrowser.SelectedPath 

WriteLine引起是這樣的方法不是一個屬性。語法應該是WriteLine(....)。

在任何情況下,您的代碼都將失敗,因爲Select案例中使用的StreamWriter沒有像第一種情況那樣正確初始化。
你需要這樣的事,與你預期的文件名

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(.....) 
     ..... 
+0

結合SelectedPath當實現這個得到一個錯誤,如「**錯誤:重載決策失敗,因爲沒有可訪問的‘OpenTextFileWriter’接受此數目的參數。 **「 – Izaz

+0

右鍵忘記第二個參數,它可以是布爾值True將文本追加到現有文件或布爾False以用新數據覆蓋現有文件。根據你的第一個模塊,我用True來追加 – Steve

0

看看你的第二個條件,RadioBtnselect聲明。你永遠不會告訴StreamWriter的路徑,因此你的錯誤。你確實在第一條路上給它一條路,但不在第二條路。

例如這樣的:

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