Reputation: 13
Here is the code:
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
It also happens if I reverse them and put the DialogResult.OK
in the ElseIf
when selecting a file.
How shall I proceed? Thanks for your help.
Upvotes: 0
Views: 289
Reputation: 7132
I guess, when you cancel the dialog, you want to exit procedure. In this case you just need to check if the result is Cancel
:
If OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then Exit Sub
After that line the result is OK, so you can safely get file path.
Upvotes: 0
Reputation: 66439
Call ShowDialog
once, save the result, and then check it. Currently, you're calling ShowDialog
twice, which shows the dialog to the user twice.
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
Upvotes: 1