CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jun 2007
    Location
    New Delhi
    Posts
    70

    How to prevent multiple button click?

    I have a form which has a button(sychronize all). I do some process when clicked on this button and disable it when clicked on. To disable it I have used the following code:

    Code:
     buttonSyncAll.Enabled = false;
     this.Update();
    When I click on this button it gets disabled and process starts.if I click on this button when it is disabled nothing happens unless process finished but once the process is finished it starts again. So somewhere application is keeping click event in queue and implementing it after it has finished the earlier event. Please tell me how can I make sure that until process finishes, the click event should not be saved/stored. I want that click event should only be handled when button gets enabled again.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to prevent multiple button click?

    I am pretty sure that Click messages are sent when the button is disabled, so I would look for any other reasons this may be happening. For a hack fix you could always unsubscribe from the event at the top of the handler and subscribe again when the method completes, but this should not be necessary.

  3. #3
    Join Date
    Jun 2007
    Location
    New Delhi
    Posts
    70

    Re: How to prevent multiple button click?

    I dont know how to do it.can you please tell me exactly how it can be done? I am new to C# programming. I have added event using visual studio IDE.
    Code:
    this.buttonSyncAll.Click += new System.EventHandler(this.OnClickSyncAll);

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to prevent multiple button click?

    Code:
    this.buttonSyncAll.Click -= this.OnClickSyncAll;
    "-=" instead of "+=", but again, I don't think it is a good solution.

  5. #5
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: How to prevent multiple button click?

    If the process that is started on the click event runs in the main/UI thread, your application will "hang". This means that the second click isn't processed by the form until the process completes and the button is re-activated.
    It's not a bug, it's a feature!

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to prevent multiple button click?

    I will second with that.

    You should actually re-look at how you are doing this process. May be doing it with the help of BackgroundWorker component would be nice.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to prevent multiple button click?

    +3

    If you really need to only perform the operation once, you'll need to start a separate thread. I've tried a few variations, including disabling the button and event and creating a guard block.

    All are ineffective if the button has focus and the user rapidly clicks twice.

    Test code.

    Code:
    private void buttonClicky_Click( object sender, EventArgs e )
    {
                Application.DoEvents( );
    
                if( !_inClickyMethod )
                {
                    _inClickyMethod = true;
                 //   buttonClicky.Click -= new EventHandler( buttonClicky_Click );
    
                    buttonClicky.Enabled = false;
    
                    
                    System.Threading.Thread.Sleep( 5000 );
    
    
    
    
                    Debug.WriteLine( "Clicky" );
    
                    buttonClicky.Enabled = true;
    
    
                    _inClickyMethod = false;
                   // buttonClicky.Click += new EventHandler( buttonClicky_Click );
    }

  8. #8
    Join Date
    Jun 2007
    Location
    New Delhi
    Posts
    70

    Re: How to prevent multiple button click?

    I also tried to take the focus away from the button but it is of no use. it starts acting on the click event only once it is finished with the earlier click event so every solution seems to fail. I don't know why it is so. in MFC if you disable a button it does not receive any event. So behavior of buttons seems very strange is c#.

  9. #9
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to prevent multiple button click?

    You do not get click events when buttons are enabled, really, you don't. You must be clicking fast enough to push two messages onto the queue. Just take the others advice and do what you need to do in a separate thread.

  10. #10
    Join Date
    Jun 2007
    Location
    New Delhi
    Posts
    70

    Re: How to prevent multiple button click?

    thanks to all of you. I changed the code to use a separate thread. But still I am surprised that this feature has already been implemented it MFC then why it is not in C# .net.

  11. #11
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to prevent multiple button click?

    Quote Originally Posted by john_avi View Post
    thanks to all of you. I changed the code to use a separate thread. But still I am surprised that this feature has already been implemented it MFC then why it is not in C# .net.
    What? What "feature"? Disabled buttons do not fire Click events. Seriously, they don't.

  12. #12
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to prevent multiple button click?

    Actually BigEd, the problem has been misunderstood. You are right disabled buttons do not fire click events, but in this case, as the control does not return back to the form, the button stays enabled because of the long process that is running in the button's click event handler. This is a problem with the code and not with .NET framework or C#.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to prevent multiple button click?

    Shula, I respectfully disagree.

    From the test code I posted (in one of several variations), I believe the click requests are getting queued up as BigEd proposed.

    For my tests, I made sure the button in question had focus and then I rapidly double clicked on it.

    Here's the sequence:
    Button disabled
    Pause for 5 seconds (due to Sleep statement)
    Debug.Writeline "Clicky" appeared in the output window
    (2nd click event processed)
    Pause for 5 seconds
    Debug.Writeline "Clicky" appeared in the output window
    Button enabled

    When the button is rapidly clicked, the two button events are getting queued before the button is initially disabled.

  14. #14
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to prevent multiple button click?

    I didn't see your code. You are using Application.DoEvents(), why is that? What happens when you remove this statement?

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to prevent multiple button click?

    Quote Originally Posted by Shuja Ali View Post
    I didn't see your code. You are using Application.DoEvents(), why is that? What happens when you remove this statement?
    I tried it all sorts of ways - the way I posted was just one of the tests. None matter.

    Test 1
    Code:
    private void buttonClicky_Click( object sender, EventArgs e )
    {
                    buttonClicky.Enabled = false;
                   
                    System.Threading.Thread.Sleep( 5000 );
    
                    Debug.WriteLine( "Clicky" );
    
                    buttonClicky.Enabled = true;
    }
    Test 2
    Code:
    private void buttonClicky_Click( object sender, EventArgs e )
    {
                    buttonClicky.Click -= new EventHandler( buttonClicky_Click );
    
                    buttonClicky.Enabled = false;
    
                    System.Threading.Thread.Sleep( 5000 );
    
                    Debug.WriteLine( "Clicky" );
    
                    buttonClicky.Enabled = true;
    
                    buttonClicky.Click += new EventHandler( buttonClicky_Click );
    }
    Test 3
    Code:
    private void buttonClicky_Click( object sender, EventArgs e )
    {
                    buttonClicky.Enabled = false;
    
                    buttonClicky.Click -= new EventHandler( buttonClicky_Click );
    
                    System.Threading.Thread.Sleep( 5000 );
    
                    Debug.WriteLine( "Clicky" );
    
                    buttonClicky.Click += new EventHandler( buttonClicky_Click );
    
                    buttonClicky.Enabled = true;
    }
    Test 4
    Code:
    private void buttonClicky_Click( object sender, EventArgs e )
    {
                    buttonClicky.Enabled = false;
    
                    buttonClicky.Click -= new EventHandler( buttonClicky_Click );
    
                    Application.DoEvents( );
    
                    System.Threading.Thread.Sleep( 5000 );
    
                    Debug.WriteLine( "Clicky" );
    
                    buttonClicky.Click += new EventHandler( buttonClicky_Click );
    
                    buttonClicky.Enabled = true;
    }
    Test 5
    Code:
    private void buttonClicky_Click( object sender, EventArgs e )
    {
                if( !_inClickyMethod )
                {
                    _inClickyMethod = true;
    
                    buttonClicky.Click -= new EventHandler( buttonClicky_Click );
    
                    buttonClicky.Enabled = false;
    
                    
                    System.Threading.Thread.Sleep( 5000 );
    
                    Debug.WriteLine( "Clicky" );
    
                    buttonClicky.Enabled = true;
    
                    _inClickyMethod = false;
                    buttonClicky.Click += new EventHandler( buttonClicky_Click );
                }
    }
    Test 6
    Code:
    private void buttonClicky_Click( object sender, EventArgs e )
    {
                Application.DoEvents( );
    
                if( !_inClickyMethod )
                {
                    _inClickyMethod = true;
    
                    buttonClicky.Enabled = false;
    
                    
                    System.Threading.Thread.Sleep( 5000 );
    
                    Debug.WriteLine( "Clicky" );
    
                    buttonClicky.Enabled = true;
    
                    _inClickyMethod = false;
                }
    }
    .and maybe a few others.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured