2013-05-04 52 views
0

我正在尋找一個「另存爲」對話框來處理我在Visual Basic中創建的應用程序。如何在Visual Basic中執行另存爲(V11 2012)

這是我使用的代碼,我不明白它的代碼或者:

Private Sub MenuExportTo_Click(ByVal sender As Object, _ 
      ByVal e As System.EventArgs) Handles MenuExportTo.Click 

     Dim myStream As IO.Stream 'I don't understand this line 
     Dim saveFileDialog1 As New SaveFileDialog() 'I don't understand this line 


     saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" 
     'I understand this 
     saveFileDialog1.FilterIndex = 1 ' I understand this 
     saveFileDialog1.RestoreDirectory = True ' I understand this 

     If saveFileDialog1.ShowDialog() = DialogResult.OK Then 
      ' I don't understand the rest 

      myStream = saveFileDialog1.OpenFile() 
      If (myStream IsNot Nothing) Then 
       ' Code to write the stream goes here. (This was what the example 
I referenced put here. the important code goes here, but the example was no help) 
       myStream.Close() 
      End If 
     End If 

問題1:我在這個新的,沒有源給了我一個深入足夠的解釋瞭解真正的節省方法。我需要一個堅實的解釋,我迷失在這裏。

問題2:我可以得到代碼寫在一個位置的文本文件,但返回脫字符都將丟失,並將其保存到文本文件都在同一行,用這樣的:

My.Computer.FileSystem.WriteAllText("c:\savelocation", TextBox1.Text, False 

如何我是否修復retun caret問題?另外, 我該怎麼做,所以用戶選擇保存位置,而不是代碼的一部分?

問題3:如何獲取用戶在另存爲對話框中指定的文件路徑到變量中?

任何幫助,即使只是我的一個問題的答案,將不勝感激! 謝謝!

回答

3

看看這個例子可以幫助:

Private Sub MenuExportTo_Click(sender As System.Object, e As System.EventArgs) Handles MenuExportTo.Click 
    Dim sfd As New SaveFileDialog() ' this creates an instance of the SaveFileDialog called "sfd" 
    sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" 
    sfd.FilterIndex = 1 
    sfd.RestoreDirectory = True 
    If sfd.ShowDialog() = DialogResult.OK Then 
     Dim FileName As String = sfd.FileName ' retrieve the full path to the file selected by the user 
     Dim sw As New System.IO.StreamWriter(FileName, False) ' create a StreamWriter with the FileName selected by the User 
     sw.WriteLine(TextBox1.Text) ' Write the contents of TextBox to the file 
     sw.Close() ' close the file 
    End If 
End Sub 
+0

運作良好,我有一個更好的理解它是如何工作。謝謝!但是,只剩下一個問題,它仍然保存在一行文本文件中。難道是因爲我正在編寫的程序會自動在文本框中輸入文本(它不是用戶輸入的)。另外,它是一個richtextbox。 – user2348797 2013-05-04 03:03:28

+0

將WordWrap設置爲False,將MultiLine設置爲True。然後,該文件應該看起來像你在TextBox中得到的。 – 2013-05-04 03:07:47

+1

如果它是** RichTextBox **,則可以使用'RichTextBox1.SaveFile(sfd.FileName)',並完全跳過StreamWriter。 – 2013-05-04 03:09:42

0
Private Sub MenuExportTo_Click(sender As System.Object, e As System.EventArgs) Handles MenuExportTo.Click 
    Dim sfd As New SaveFileDialog() ' this creates an instance of the SaveFileDialog called "sfd" 
    sfd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" 
    sfd.FilterIndex = 1 
    sfd.RestoreDirectory = True 
    If sfd.ShowDialog() = DialogResult.OK Then 
     Dim FileName As String = sfd.FileName ' retrieve the full path to the file selected by the user 
     Dim sw As New System.IO.StreamWriter(FileName, False) ' create a StreamWriter with the FileName selected by the User 
     sw.WriteLine(TextBox1.Text) ' Write the contents of TextBox to the file 
     sw.Close() ' close the file 
    End If 
End Sub 
相關問題