Hack Forums
[C#] Making a button clickable every 10 seconds. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Making a button clickable every 10 seconds. (/showthread.php?tid=4567288)

Pages: Pages 1 2


Making a button clickable every 10 seconds. - SIM-810273 - Dec 3, 2014

Hey guys how are you, so I have a game, and I want a button that when you click it the button gives the character a boost (i know how to code this) but what I can't code is once they've clicked that button they cannot click the button again for a set amount of time.

tl;dr
I'm trying to create a button which once clicked cannot be clicked again for a set amount of time.

Any help would be appreciated. Thanks in advance!


* - pac - Dec 3, 2014

Put a timer which enables and disables the button every 10 seconds. I believe threading would also do fine. After your button logic is done, you disable the button, add Thread.Sleep(10000) and re-enable it again.


* - Niggagetsbannedinunder10 - Dec 3, 2014

Edit: im stupid and was looking in the wrong section.


* - Carlsson94 - Dec 3, 2014

(Dec 3, 2014 07:48 AM)pac Wrote:  Put a timer which enables and disables the button every 10 seconds. I believe threading would also do fine. After your button logic is done, you disable the button, add Thread.Sleep(10000) and re-enable it again.

Except Thread.sleep would sleep the whole application just not the button.


* - Malware - Dec 3, 2014

On a new thread, also turn illegalcrossthreading off

while (true)
{
Thread.sleep(10000);
button1.enabled = !button1.enabled;
}


* - Hawk HF - Dec 3, 2014

(Dec 3, 2014 07:48 AM)pac Wrote:  Put a timer which enables and disables the button every 10 seconds. I believe threading would also do fine. After your button logic is done, you disable the button, add Thread.Sleep(10000) and re-enable it again.

Using Thread.Sleep() would freeze the entire application for 10 seconds. Not just the button.

(Dec 3, 2014 08:02 AM)Venomous Net Wrote:  On a new thread, also turn illegalcrossthreading off

while (true)
{
Thread.sleep(10000);
button1.enabled = !button1.enabled;
}

There is no point in creating a new thread just for this... And your code would enable and disable the button every 10 seconds, not on the click event as OP asked for.




Just use a timer with a 10 second interval.
The Tick() event will fire after 10 seconds. Re-enable the button then.


* - Odus - Dec 3, 2014

(Dec 3, 2014 08:14 AM)Hawk HF Wrote:  Just use a timer with a 10 second interval.
The Tick() event will fire after 10 seconds. Re-enable the button then.

Take note everyone, no need to over complicate!
Code
private void button1_Click(object sender, EventArgs e)
      {
      button1.Enabled = false;
      timer1.Start();
      }
private void timer1_Tick(object sender, EventArgs e)
      {
      button1.Enabled = true;
      timer1.Stop();
      }



* - ShadowBoi - Dec 3, 2014

If you guys are going to tell him to use the Thread.Sleep() function you should start by explaning how to actually make a thread then. Because else will it sleep the main thread and that will freeze the whole program.


* - CrypticX - Dec 3, 2014

Why not just

PHP Code

<?php 
private void button1_Click(object sender, EventArgs e)
      {
      
//code
      
button1.Enabled = false;
      
Thread.Sleep(miliseconds);
      
button1.Enabled = true;
      } 

Or yeah you can do it with a timer too as mentioned above.


* - чистая душа - Dec 3, 2014

Please dont use thread.sleep() if you really need this feature check system time. then have a whille loop that says while currenttime < time + 10
buutton is unclickable


* - Old_Horse - Dec 3, 2014

The correct way to do it is:

Code
var timer = new Timer { Interval = 10000 };
t.Tick += delegate { button1.Enabled = !button1.Enabled; };
t.Start();



* - Agent Raigar - Dec 3, 2014

Quote:also turn illegalcrossthreading off

Please don't.
Also,
(Dec 3, 2014 03:40 PM)Old_Horse Wrote:  The correct way to do it is:

Code
var timer = new Timer { Interval = 10000 };
t.Tick += delegate { button1.Enabled = !button1.Enabled; };
t.Start();

There isn't a correct or wrong way to do this.


* - Old_Horse - Dec 3, 2014

(Dec 3, 2014 03:48 PM)Agent Raigar Wrote:  Please don't.
Also,

There isn't a correct or wrong way to do this.

Okay, let me paraphrase:

The easiest and cleanest way to do it.


* - Corios - Dec 3, 2014

(Dec 3, 2014 03:48 PM)Agent Raigar Wrote:  Please don't.
Also,

There isn't a correct or wrong way to do this.

actually yes there is, because this will turn the button on and off every 10 seconds. Not what the OP wants. What if he doesn't click it in the 10 seconds? it'll disable it for the next 10.


* - Old_Horse - Dec 3, 2014

(Dec 3, 2014 04:06 PM)Corios Wrote:  actually yes there is, because this will turn the button on and off every 10 seconds. Not what the OP wants. What if he doesn't click it in the 10 seconds? it'll disable it for the next 10.

Oops, my bad. That's what you get for only reading the title. 🤦‍♂