2 Message Boxes Displayed when User Clicks Button

J

jburkle

The following is the onclick method called when the "Renew" button is
clicked by the user in my Windows application:

Code:
.....

Private Sub cmdRenew_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdRenew.Click

Try
If x Is Nothing Then
Exit Sub
End If
If y > 0 Then
If z = 0 Then
MsgBox("Random Message")
Exit Sub
End If
Else
Exit Sub
End If
If a Then
MessageBox.Show("Random Message")
Exit Sub
End If
If (b Or c) Then
MsgBox("Random Message")
Exit Sub
Else
If d Then
...
If e Then
MsgBox("Random Message")
Exit Sub
End If
Else
If f Then
...
If g Then
MsgBox("Random Message")
Exit Sub
End If
Else
MsgBox("Random Message")
Exit Sub
End If
End If
End If

''Problem Here

If (h = 123) Then
MsgBox("This Message Displays to the User while Done!
MessageBox is also being Displayed")
Exit Sub
End If
If i <> "" Then
...
If j Then
MsgBox("Random Message")
Exit Sub
End If
If k Then
...
Else
If l Then
...
Else
...
End If
End If

''Gets to here
h = 123
DoEvents()
MessageBox.Show("Done!", "", MessageBoxButtons.OK)
Else
MsgBox("Random Message")
End If
Catch ex As Exception
ErrorTrap(ex,
System.Reflection.MethodInfo.GetCurrentMethod.Name, "")
End Try
End Sub

At first I didn't have the DoEvents() call before the Done! MessageBox
was displayed, but the Done! box would be displayed in the background
of the form and the user couldn't get to it withou ALT+TAB-ing to it. I
attempted to fix that and found that adding the DoEvents() call before
the Message Box was displayed fixed it, but now sometimes when the user
clicks the button, the Done! messagebox is displayed then the
MsgBox("This Message Displays to the User while Done! MessageBox is
also being Displayed") is displayed and locks up the whole program,
both that MEssage Box and the Done! message box are reached using the
ALT+TAB and the program freezes up. It seems as thought it runs through
the whole onclick, sets h = 123, displays the Done! message box, then
loops the onclick method again and gets to the h=123 check and sees it
now as true and displays that message box too.

Any thoughts, if it is the DoEvents() call that is causing it then does
anyone have a solution as to why the Done! message box is only
reachable using ALT+TAB by the user without the DoEvents() call?
 
C

Chris Dunaway

jburkle said:
The following is the onclick method called when the "Renew" button is
clicked by the user in my Windows application:

Code:
.....

Private Sub cmdRenew_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles cmdRenew.Click
[/QUOTE]

[QUOTE]
''Problem Here

If (h = 123) Then
MsgBox("This Message Displays to the User while Done!
MessageBox is also being Displayed")
Exit Sub
End If[/QUOTE]

[QUOTE]
''Gets to here
h = 123
DoEvents()
MessageBox.Show("Done!", "", MessageBoxButtons.OK)
Else
MsgBox("Random Message")
End If[/QUOTE]

It seems like your event handler is being called twice.  Can you put
MessageBox.Show("Button Clicked!") at the top of the click event to
confirm this?

If it gets called twice, do you use the AddHandler statement anywhere?
 
J

jburkle

It does seem like the handler is being called twice. Is it a possiblity
that a double-click on the button could cause it to be called twice, or
could the Application.DoEvents() cause it?
 
J

jburkle

it turns out that when you call the DoEvents() it allows the user to
click on the button again and then it will loop through the onclick
method once it completes the first time. To fix this I added
Me.form.enable = false at the beginning of the onclick so that once it
enters the method and does the DoEvents() the user won't be able to
click on the button again.... then once the method is complete i set
Me.form.enable = true.

if anyone out there has a "Better" way to fix this then please let me
know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top