Stop multiple buttons clicks

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?
LVL 1
jdhackettAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
"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"...
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Try to do

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

and then your search code
0
 
jdhackettAuthor Commented:
Just tried that, doesn't work.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Bill-HansonCommented:
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.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
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
0
 
VBRocksCommented:
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
0
 
Bill-HansonConnect With a Mentor Commented:
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.
0
 
jdhackettAuthor Commented:
Thanks guys. Idle_Mind - that worked great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.