dialog opens twice?

Xentrolis

New member
Joined
Aug 13, 2010
Messages
3
Location
The Abyss lolol
Programming Experience
Beginner
Hello, my name is Xentrolis and I'm new to this forum, and fairly new to the VB.NET programming language.

My problem is with and Open File Dialog. I've just started learning VB very recently, following tutorials at Microsoft Visual Basic .NET tutorials for Beginners, so the "application" I'm making is a very simple one and doesn't function fully as it is supposed to, however let's get down to business:

I'm trying to test whether the Open button or the Cancel button in an OpenFileDialog was clicked. I want one of two things to happen:

1. If the Cancel button is clicked, I want to display a messagebox saying "Cancel Button Clicked"
-or-
2. If the Open button is clicked, I want to disoplay a messagebox showing the path and file name of the .txt file selected.

I've basically got it working, except for one small querk. When you click File>Open, it opens an OpenFileDialog, as it should, however whether you click Open or Cancel, it will display the messagebox showing what I want it to, however another OpenFileDialog pops up directly after you click either button.

I'm using Microsoft Visual Basic 2010 Express, in case you needed to know.

Any and all help is appreciated, and please try not to flame or troll me just because I'm new to this forum. I'm not new to the internet, and it will not work. If you have nothing helpful or constructive to say, do us both a favor and don't say anything at all.
 
There's obviously an issue with your code but, as we can't see your code, we can only guess at what that might be. In situations like this you need to post the relevant code. Only if we see what you're doing can we see what you're doing wrong.

That said, it's the ShowDialog method that shows the dialogue. If you're showing the dialogue twice then you must be calling ShowDialog twice. That should be what you look for first.
 
I would be willing to bet you saw how to check for ok with a statement like
if yourForm.ShowDialog=DialogResult.OK Then
commands
End If

If you use the same if statement for Cancel, you will get a second dialog box. The reason is that yourForm.ShowDialog is a method that causes the dialog form to show. It also returns the result upon closing. For each time you call the method, a new form is shown. Here are a few ways to check for more than 1 possible result.


Use a Select Statement

Select Case yourForm.ShowDialog
Case Windows.Forms.DialogResult.OK
'Commands for OK
Case Windows.Forms.DialogResult.Cancel
'Commands for cancel
Case Else
'Commands for any other result
End Select

If the only Options are ok or cancel then use an Else Statement

If yourForm.ShowDialog = Windows.Forms.DialogResult.OK Then
'Commands for OK
Else
'Commands for Cancel
End If

End IF


You could also define a variable as the result

Dim result As Windows.Forms.DialogResult
result = yourForm.ShowDialog
If result = Windows.Forms.DialogResult.OK Then
'Commands for OK
End If
If result = Windows.Forms.DialogResult.Cancel Then
'Commands for cancel
End If
 
I ended up doing it with an if/else statement and a variable, something like:

Dim openFDresult As openFD.ShowDialog

If openFDresult = DialogResult.Open Then
\\commands for Open\\
Else
\\commands for Cancel\\
End If

Thanks for the help though.
 
Back
Top