473,395 Members | 2,006 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,395 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 10492
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.