Disable a button for few seconds?

How do you disable a button for 10 seconds?
SendoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

viktornetCommented:
var
  i : Integer;
begin
....
  Button1.Enabled := False;
  for i := 0 to 9 do begin
    Sleep(1);
    Application.ProcessMessages;
  end;
  Button1.Enabled := True;
....
end;

..-=ViKtOr=-..
Hamlet081299Commented:
A TTimer might be a nicer way to do this, but is a bit more complicated.

Also Sleep takes milliseconds so you would have to make this Sleep(1000) to go in 1 second blocks.

.... and a try finally would be good to make sure that the button still gets enabled if there are exceptions in the meantime.

BlackDeathCommented:
complicated's relative...

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Timer1: TTimer;
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure DisableBtn(t: Integer);
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DisableBtn(t: Integer);
begin
  Button1.Enabled := False;
  Timer1.Interval := t;
  Timer1.Enabled := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  DisableBtn(5000);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  Button1.Enabled := True;
end;

end.

- hit button2 2 disable button1 4 5 seconds (5000 ms).

;-)

BlackDeath.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

SendoAuthor Commented:
What has App.Pr.Mess. to do with disabling a button?
I do not want to click to diasble a button ..
I want the button to be disabled (on show)for 5 sec. before I can use it for other purpose...
I think TTimer is the answer ...

Button1.Enabled:=False;
Then Timer thing
Timer1.Enabled:=false;
Timer1.Interval := 5;
Button1.Enabled:=True;
Timer1.Free;
Something like it ...How?
(I did it once but ...amnesia...)

viktornetCommented:
Sendo, Application.ProcessMessages() has A LOT to do with it so please try the code FIRST and then do your talking. Here is my exact answer again. Just place the code in your OnShow() of the form.

var
  i : Integer;
begin
  ....
  Button1.Enabled := False;
  for i := 1 to 10000 do begin
    Sleep(1);
    Application.ProcessMessages;
  end;
  Button1.Enabled := True;
  ....
end;

btw, I changed a few lines of the code so it stays disabled for 10 mins as it should.

..-=ViKtOr=-..
Hamlet081299Commented:
Application.ProcessMessages just allows the system to process other messages ... you need this if you want the user to still be able to use the application during the 10 seconds that the button is disabled.

I'd also suggest a modification to victornet's code if you want to go with that ...

for i := 1 to 100 do begin
  Sleep(100);
.... etc

Sleeping for 1 millisecond and doing Application.ProcessMessages 10,000 times will not equate to 10 seconds, because the Application.ProcessMessages itself can take some time.  What I've suggested will be closer, and still allow a reasonable response for user activity.

Even better go with the timer idea, as I suggested before, it's probably worth the extra effort.  BlackDeath's code sample looks suitable, but it depends whether this is a one off or not.

If you want to use this sort of feature on more than one button or on more than one control even, then the answer would be different.

Some idea as to why you want to disable the button would help me give you the best answer I can.
WillyDharmaCommented:
Try use TTimer object, give it's interval to 10000
(1000 for 1 second), and make button disable at first use.

Procedure TForm1.Timer1Timer(Sender : TObject);
Begin
   Button1.Enabled := True;
   Timer1.Enabled := False;
End;

Procedure TForm1.Button1Click(Sender : TObject);
Begin
   Button1.Enabled := False;
   Timer1.Enabled := True;
End;

it's from BlackDeath's code sample, and i make it more simple.
Hamlet081299Commented:
Here's an option which will accurately disable the button for as close to 10seconds as possible.

procedure TForm1.FormActivate(Sender: TObject);
var
  tStop: integer;
begin
  Button1.Enabled := False;
  tStop := GetTickCount + 10000;
  while GetTickCount < tStop do
    Application.ProcessMessages;
  Button1.Enabled := True;
end;

By the way DON'T put this code in the OnShow event.  If you did that your form won't appear for 10 seconds.  This is also the case with Sendo's answer.

One other minor thing ... GetTickCount wraps every 49.7 days so if you were to execute this code exactly 49.7 days after the computer was last rebooted then it would not pause for 10 seconds but return immediately.  You can code around that if you really want to ... but what are the chances?

Hamlet081299Commented:
Oops I meant to refer to victornet's code not sendo's.

A couple more notes ...

Do not do what victornet said and put the code in OnShow ... your form won't show for 10 seconds.

If you have code like mine or victornet's on your MainForm, and try to close the application it will not close until the time is up.  In this case change the code to ...

  while GetTickCount < tStop do begin
    Application.ProcessMessages;
    if Application.Terminated then
      BREAK;
  end;

.... this will ensure that the code works correctly on the main window also.
viktornetCommented:
Hamlet, NOT TRUE AT ALL. What do you think Application.ProcessMessages is all about? It says it all. Of course the application will close if you try to close while the button is disabled. And the form should show if you put the code in OnShow, so what Hamlet is saying is completely wrong. I haven't tried the code I've given but my beliefs are that the code is gonna work just as it is. If not let me know, and I will change it appropriately.
Hamlet081299Commented:
viktornet

I was a bit surprised at the results when I TESTED THE CODE.

If you have tested this code and it verfies your opinion then I would be surprised!  On my machine it performed exactly as I said.  I wouldn't have said it if I hadn't tested it.

If you bother to test it and it does do what you say let me know, because I'd be curious as to why it operates differently on your machine.  If it helps I'm running D4 on NT 4.

You abviously have a great deal of self-confidence to make such bold statements without even testing it.  Did you test your loop with the Sleep(1) inside a loop.  My ACTUAL RESULTS showed this to be totally inaccurate.
viktornetCommented:
can't bother to test it now...

>>self-confidence
im not self-confident... i'm rather lazy...

>>bold statements
not bold at all....

-vik-
BlackDeathCommented:
sendo - u don't have 2 click 2 disable the button.
my code was just an example. u can use the disablebtn-proc wherever u want, it doesn't necessarily have 2 b in a buttonclick event...

as far as vik's application.processmessages is concerned - if u use a loop 2 disable the button, vik's right: it's necessary in order 2 not let the app do nothing w/in the time.
if u use a timer (as i suggested), it is not necessary.

would b nice if u'd give ur opinion on the comments here.

willydharma - of corze u made it simpler. & less flexible, of corze... ;-)

vik - hi, vik. long time no hear. how r things goin?

;-)

BlackDeath.
viktornetCommented:
hey BD, yep, long time no hear...

well, letz C. thingz aint goin very well.. school started a week ago or maybe more and i aint got time 2 do nuttin... i do some win32 asm programming (and some DOS) and that's all the time i spend on the computer. reading about game creation at times and surfing the net late at night when i fall asleep on the keyboard.

..-=ViKtOr=-..
SendoAuthor Commented:
Hi folks!
Sorry I could not reply earlier.My email box got
screwed up...
Points to BD as it is detailed and simple.
All other sugg. work too ....
Thank you all...

BlackDeathCommented:
heyyy, another 20 pts on my way straight up 2 the top harharhar... ;-)

thanxalot, sendo.
pleasure 2 help ya.

h.a.n.d.,

BlackDeath.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.