We help IT Professionals succeed at work.

Stop multiple buttons clicks

jdhackett
jdhackett asked
on
Medium Priority
202 Views
Last Modified: 2010-04-23
Vb Net 2005
I run a large database search when a button is clicked. Takes several seconds. If the user presses the button while the search is taken place, then the search happens again.
I've tried
Me.cmdFind.Enabled = False
as the first line of cmdFind_Click, but the extra clicks are still stored somewhere.
How do I prevent this?
Comment
Watch Question

Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
Try to do

Me.cmdFind.Enabled = False
Application.DoEvents()

and then your search code

Author

Commented:
Just tried that, doesn't work.
Place the search code into a separate thread.  When the user clicks the search button, check the thread to see if it is running.  If it is, display a nice message and return.  If it is not running, start it.
Jorge PaulinoIT Pro/Developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
If this doesn't work you can ...

Private SecondClick as Boolean

Sub MySub()
    If Not SecondClick Then
        SecondClick = True
            ' your code
        SecondClick = False
    End If
End Sub
Top Expert 2007

Commented:
Hey, jpaulino got Master!  Congratulations!

Here's an alternative suggestion:

    Dim bFirstClick As Boolean = True
    Private Sub cmdFind_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        If bFirstClick = True Then
            bFirstClick = False

            'Process...

        End If
    End Sub
None of this is going to work because the clicks are queued by the Windows event manager until your application becomes responsive again.

After your search routine completes, your application starts processing the events that were queued during the serach process.

The only way out of this is to pump the queued messages out of the Windows Message Queue at the END of your search or to place the search code into a new thread.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
"The only way out of this is to pump the queued messages out of the Windows Message Queue at the END of your search"

Exactly Bill-Hanson.

To do this, place a call to Application.DoEvents() at the BOTTOM of your button handler...but BEFORE re-enabling the button and you should be good to go.

This snippet demonstrates this in action:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False

        For i As Integer = 1 To 500
            Debug.Print(i)
        Next

        Application.DoEvents()
        Button1.Enabled = True
    End Sub

If you comment out the DoEvents() line then you can click the disabled button and it will fire the loop again.  Uncomment the line and it will not "repeat"...

Author

Commented:
Thanks guys. Idle_Mind - that worked great.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.