Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Making a button clickable every 10 seconds.

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!
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.
Edit: im stupid and was looking in the wrong section.
(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.
On a new thread, also turn illegalcrossthreading off

while (true)
{
Thread.sleep(10000);
button1.enabled = !button1.enabled;
}
(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)Malware 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.
(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();
      }
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.
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.
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
The correct way to do it is:

Code
var timer = new Timer { Interval = 10000 };
t.Tick += delegate { button1.Enabled = !button1.Enabled; };
t.Start();
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.
[Image: irDbVUN.gif]
(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.
(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.
(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. 🤦‍♂


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by LeonieHackPetaBytes2Jan 23, 2019 05:10 PM 1 227 Jan 23, 2019 05:10 PM
Last Post: 0xDEAD10CC
[Shell] by ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™Nov 26, 2018 05:48 AM 9 406 Nov 26, 2018 05:48 AM
Last Post: pms
[C#] by KaleMar 7, 2016 05:01 PM 4 186 Mar 7, 2016 05:01 PM
Last Post: Kale