473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Disabled button calling even handler on button click

Hi
I am having button on form in C#. It does take 2-3 seconds to complete the operation meanwhile the button is disabled. But if the user clicks on that button when it is disabled, after completion of first operation it will again try to execute the handler again. how should I avoid the execution of event handler when the button is disabled?

here is the code snippet: (Refer the attachment for test application)

Expand|Select|Wrap|Line Numbers
  1. public partial  class Form1  : Form
  2.     {
  3.         int i = 0;
  4.         public Form1()
  5.         {
  6.             InitializeComponent();
  7.         }
  8.  
  9.         private void button1_Click(object sender, EventArgs e)
  10.         {
  11.             this.button1.Enabled = false;
  12.             System.Threading.Thread.Sleep(3000);
  13.             i++;
  14.             this.label1.Text = "Total Calls: " + i.ToString();
  15.             this.button1.Enabled = true;
  16.         }
  17.     }
when the button is disabled and you click on it, you can see the count gets increased.

Please help
Attached Files
File Type: zip DisableProblemEx.zip (12.0 KB, 149 views)
May 12 '10 #1
5 10480
GaryTexmo
1,501 Expert 1GB
I believe it's because you've had the thread sleep so that any input on it is buffered and released when the thread is done sleeping.

Try using a timer instead... set the interval to whatever you like (probably 3 seconds as you're doing with the sleep), then disable the button in the click call and start the timer. In the timer tick event handler, stop the timer and enable the button.
May 12 '10 #2
tlhintoq
3,525 Expert 2GB
You can also unsubscribe the button from the handler while it is disabled. That way even if the press event is stored it goes nowhere.
May 12 '10 #3
ThatThatGuy
449 Expert 256MB
@sandeepkavade
Try tlhintoq's suggestion of unsubscrbing from the event...

e.g.
Expand|Select|Wrap|Line Numbers
  1. Button1.Click-=new MouseEventArgs(Button1_Click);
  2.  
May 13 '10 #4
GaryTexmo
1,501 Expert 1GB
The only reason I'd still advocate using a timer instead of a Thread.Sleep (disable button or remove the handler, either doesn't matter) is so your form is still responsive for the duration of your operation, which should also be done in another thread (which sounds like it might be since you're doing a sleep).

I say this because it's generally a bad thing when your form becomes unresponsive for whatever reason. It can be frustrating for the user and, in some cases, it can be dangerous if the application controls something important and control is locked out because the form is tied up doing a task.

Obviously you'll do what's right for you, but it's something to think about :)
May 13 '10 #5
If you call Appliction.DoEvents() before enabling button app will work as you intended.

Reason clicks get through is they pile up in messages queue, which is processed after your method ends, and button is enabled by that time. Processing messages queue before enabling button solves it.
Apr 3 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Adrian | last post by:
I need to have 2 submit buttons in one form calling the same page. I just need to know which was used. Being able to pass a hidden form input for each would be ideal. How can I do this?? ...
5
by: cwm137 | last post by:
I am trying to call the click event of a button located in a subform called "Order Subform" from a button in the parent. However, since the subform was made with the wizard, it contains a space in...
1
by: vulgate | last post by:
i use arg.button==( MouseButtons.Left+MouseButtons.Right) but the system promote the the type cant + who can help me ~
2
by: Andrew Banks | last post by:
I've got the following code as part of a C# web form but am having problems calling a command. I create a dataset and put some data on the screen. This works fine. (relevant sample below) ...
0
by: harmeet | last post by:
hi all, i am implementing command pattern in my project and adding event handler for tollbar buttonclick event. while i click on any button it notify multiple times due to multiple event handler...
3
by: Russell | last post by:
I have a web page that i am using an image button as my submit button. The code behind the button works, but when I click enter on the web page, nothing happens. Can anybody tell me how to get my...
4
by: Mark Olbert | last post by:
I have a website (ASPNET2) where the master page has a button in it and my login page, which is a detail page of the master page, also has a button (a login button). Everything works fine if I...
4
by: Nitin | last post by:
Hey Guys, I have a problem. I need to call event handler of buttons placed on a form on its keypress event. The Problem is i need to read a file which includes the button name whose click event is...
1
by: H F | last post by:
Hi all I created a user control that has a button and have added it to the Master page. When I click on the button, the onClick event handler in the user control is not fired. How do I reference...
1
by: mazdotnet | last post by:
Hi all, I'm really stuck trying to figure the following out. I have been working on a shopping portal where you can select products from a list inside a repeater on the bottom of the page and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.