[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

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?
0
jdhackett
Asked:
jdhackett
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
jpaulinoCommented:
Try to do

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

and then your search code
0
 
jdhackettAuthor Commented:
Just tried that, doesn't work.
0
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jpaulinoCommented:
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-HansonCommented:
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
 
Mike TomlinsonMiddle 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
 
jdhackettAuthor Commented:
Thanks guys. Idle_Mind - that worked great.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now