honar abubakr

honar abubakr

  • NA
  • 73
  • 110.8k

disable all form buttons in a given duration?

Mar 10 2014 9:09 AM
I have a form in C# which has 4 buttons. I would like to disable all 4 buttons after a given duration of time, for example in 1 or 2 minutes. I searched in Google and found this code but it does not seem to work:
private System.Timers.Timer aTimer = new System.Timers.Timer(60) { AutoReset = false };
 
protected void Timer2_Tick(object sender, EventArgs e)
{ aTimer = new System.Timers.Timer(60);
 aTimer.Interval = 60; double counter = aTimer.Interval;
 counter
++; if (counter >= 60) {
 lib_bt
.Enabled = false;
 MessageBox.Show("Time Up!");
}
}

Answers (3)

0
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Mar 10 2014 9:50 AM
If I were you, unless you're using multi-trheading, I'd use the System.Windows.Forms.Timer in a Windows Forms application. Just drag it from the ToolBox and double click on it to generate a Tick eventhandler:

private void Form1_Load(object sender, EventArgs e)
{
    timer1.Interval = 60000; // 1 minute in milliseconds
    timer1.Enabled = true; // start timer 
}

private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Enabled = false; // stop timer
    button1.Enabled = false;
    button2.Enabled = false;
    button3.Enabled = false;
    button4.Enabled = false;
}


Accepted Answer
1
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Mar 10 2014 10:17 AM
Sure, if you want to enable them again after another minute, then change the code in timer1_Tick to the following which will toggle the buttons' Enabled state every minute:

private void timer1_Tick(object sender, EventArgs e)
{
    button1.Enabled = !button1.Enabled;
    button2.Enabled = !button2.Enabled;
    button3.Enabled = !button3.Enabled;
    button4.Enabled = !button4.Enabled;
}


0
honar abubakr

honar abubakr

  • 0
  • 73
  • 110.8k
Mar 10 2014 10:02 AM
thank you for your answer ... can you tel me how i can enable this buttons after it disable have you any idea?