仅针对多种不同情况使用一个OpenFileDialog

时间:2016-10-20 06:46:29

标签: vb.net openfiledialog

我有一个使用多个OpenFileDialog的程序,但我想使用一个。{我编写了一个代码来执行此操作,但有更简单的方法吗?

Public Class Form1
    Private _intFlag As Integer = 0

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        _intFlag = 1
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        _intFlag = 2
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        If _intFlag = 1 Then
            TextBox1.Text = OpenFileDialog1.FileName
        ElseIf _intFlag = 2 Then
            TextBox2.Text = OpenFileDialog1.FileName
        End If
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

我建议不要处理FileOk事件。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        TextBox1.Text = OpenFileDialog1.FileName
    End If
End Sub

和其他Button类似。