2016-10-09 55 views
0

我有這段代碼,我想使它成爲一個函數,所以我可以通過改變一些參數來重用它。創建一個打開文件對話框功能多次使用

因此,這是我迄今爲止

Sub OpenFiles(InFile As IO.StreamReader, verifytheFileName As String,dialogBoxTitle As String) 
    Dim result As DialogResult 
    Dim FilePath As String 
    Dim FileName As String 

    Try 
     dialogBox1.Title = dialogBoxTitle 
     dialogBox1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 
     result = dialogBox1.ShowDialog 'Open This Dialog Box 
     FilePath = dialogBox1.FileName 'Gets the Path of the file and stores it in File Path Varible 
     FileName = dialogBox1.SafeFileName 'Gets the name of the file and stores it in File Name Varible 

     If Not FileName = verifytheFileName Then 
      MessageBox.Show("Please Select " &verifytheFileName) 
      dialogBox1.Reset() 
     Else 
      InFile = IO.File.OpenText(FilePath) 
     End If     
    Catch ex As Exception 
     MessageBox.Show("ERROR") 
    End Try 

End Sub 

所以,我不能這樣做的唯一事情就是改變以dialogBox2dialogBox3dialogBox1文本。

我如何能得到這個工作

+1

爲什麼你需要去改變它,請建議嗎?它可以正常工作,除非您想同時打開多個FileOpen對話框,這是沒有意義的,並且只能從多個線程中進行。 – GSerg

+0

我有幾個按鈕可以打開不同類型的文件 – jake123

+1

無論如何您都無法一次按下它們。只要將'Filter'作爲參數傳遞,就像已經通過'Title'一樣。 – GSerg

回答

1

傳遞對話框中設定的參數進行

Sub OpenFiles(InFile As IO.StreamReader, verifytheFileName As String,dialogBoxTitle As String, dialogBox1 as System.Windows.Forms.SaveFileDialog) 
Dim result As DialogResult 
Dim FilePath As String 
Dim FileName As String 

Try 
    dialogBox1.Title = dialogBoxTitle 
    dialogBox1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*" 
    result = dialogBox1.ShowDialog 'Open This Dialog Box 
    FilePath = dialogBox1.FileName 'Gets the Path of the file and stores it in File Path Varible 
    FileName = dialogBox1.SafeFileName 'Gets the name of the file and stores it in File Name Varible 

    If Not FileName = verifytheFileName Then 
     MessageBox.Show("Please Select " &verifytheFileName) 
     dialogBox1.Reset() 
    Else 
     InFile = IO.File.OpenText(FilePath) 
    End If     
Catch ex As Exception 
    MessageBox.Show("ERROR") 
End Try 

End Sub 
相關問題