为什么我必须取消 OpenFileDialog 两次才能关闭
Why do I have to cancel OpenFileDialog twice for it to close
代码如下:
Private Sub btn_selectfile_Click(sender As Object, e As EventArgs) Handles btn_selectfile.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files | *.txt"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'some code here
ElseIf OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
OpenFileDialog1.Dispose()
End If
End Sub
如果我在选择文件时反转它们并将 DialogResult.OK
放在 ElseIf
中,也会发生这种情况。
我该如何进行?感谢您的帮助。
调用ShowDialog
一次,保存结果,然后查看。目前,您正在调用 ShowDialog
两次,这会向用户显示两次对话框。
Dim result As DialogResult = OpenFileDialog1.ShowDialog();
If result = Windows.Forms.DialogResult.OK Then
'some code here
ElseIf result = Windows.Forms.DialogResult.Cancel Then
OpenFileDialog1.Dispose()
End If
我猜,当你取消对话框时,你想退出程序。在这种情况下,您只需要检查结果是否为 Cancel
:
If OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then Exit Sub
在那一行之后,结果就可以了,所以你可以安全地获取文件路径。
代码如下:
Private Sub btn_selectfile_Click(sender As Object, e As EventArgs) Handles btn_selectfile.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files | *.txt"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'some code here
ElseIf OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
OpenFileDialog1.Dispose()
End If
End Sub
如果我在选择文件时反转它们并将 DialogResult.OK
放在 ElseIf
中,也会发生这种情况。
我该如何进行?感谢您的帮助。
调用ShowDialog
一次,保存结果,然后查看。目前,您正在调用 ShowDialog
两次,这会向用户显示两次对话框。
Dim result As DialogResult = OpenFileDialog1.ShowDialog();
If result = Windows.Forms.DialogResult.OK Then
'some code here
ElseIf result = Windows.Forms.DialogResult.Cancel Then
OpenFileDialog1.Dispose()
End If
我猜,当你取消对话框时,你想退出程序。在这种情况下,您只需要检查结果是否为 Cancel
:
If OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then Exit Sub
在那一行之后,结果就可以了,所以你可以安全地获取文件路径。