disable a button
-
private void button1_Click(object sender, EventArgs e) { button2.Enabled = false; Thread.Sleep(2000); button2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Button 2"); } in the above code, when i click the button2 while the sleep i on, after the sleep is over, i get the message box in the button2 click event. what is the function of setting enable = false then? how to prevent this? thanks
-
private void button1_Click(object sender, EventArgs e) { button2.Enabled = false; Thread.Sleep(2000); button2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Button 2"); } in the above code, when i click the button2 while the sleep i on, after the sleep is over, i get the message box in the button2 click event. what is the function of setting enable = false then? how to prevent this? thanks
You are putting the current Thread in sleep stage so, Your button2 is disabled but when you click on it, The event is not fired. because the thread is sleep and it will not accept the click event it actually Fire the event when the thread is in running mode again so at that time the button2 is enabled. if you are still confused then reply this message
Best Regards, Chetan Patel
-
You are putting the current Thread in sleep stage so, Your button2 is disabled but when you click on it, The event is not fired. because the thread is sleep and it will not accept the click event it actually Fire the event when the thread is in running mode again so at that time the button2 is enabled. if you are still confused then reply this message
Best Regards, Chetan Patel
i dont want to fire the button2 click even when it is disabled. how do i do it?
-
i dont want to fire the button2 click even when it is disabled. how do i do it?
Hello, You have to set the "Enabled" property before the user clicks the button. If you want to make a validation on the/an event itselfe, I would suggest inherit your own Button from System.Windows.Forms.Button. Use a validation method which uses Flags or some other stuff. Override OnMouseDown, which checks the validation method and calls the base method or not! Not calling the base method of OnMouseDown, will prevent the click event from fireing! See this code example:
public class SpecialButton : System.Windows.Forms.Button { public SpecialButton() { } private bool flag1 = true; public bool Flag1 { get { return flag1; } set { if(value!=flag1) flag1 = value; } } private bool flag2 = true; public bool Flag2 { get { return flag2; } set { if(value!=flag2) flag2 = value; } } private bool ValidateClick() { //If Flag1 and Flag2 are true, the Click event will be fired return Flag1&&Flag2; } protected override void OnMouseDown(MouseEventArgs e) { if(ValidateClick()) { //Validation ok (Click will fire) base.OnMouseDown (e); } else { //give some feedback to the user (MessageBox) } } }
Hope it helps!All the best, Martin
-
private void button1_Click(object sender, EventArgs e) { button2.Enabled = false; Thread.Sleep(2000); button2.Enabled = true; } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Button 2"); } in the above code, when i click the button2 while the sleep i on, after the sleep is over, i get the message box in the button2 click event. what is the function of setting enable = false then? how to prevent this? thanks
Hi, the way you do it, button2 is not disabled at all, since the 2-second period it is disabled you also "stop the world" by holding off the GUI thread. don't put Thread.Sleep() in an event handler, it stops the GUI, which you never want to happen. Instead, disable button2 and organize something to re-enable the button later. For instance launch a Windows.Forms.Timer; when that timer expires, re-enable button2. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google