views:

511

answers:

4

hey, what do i have to write, if i want date&time which updates itself? Label3.Caption := TimeToStr(Time) this just shows me the time, when i opened the program, but i want a time, that updates to the form every second (--> like a normal clock does).

+11  A: 

1) Drop a TTimer object on your form

2) Set its 'Interval' propert to 1000 and its 'Enabled' property to true.

3) Double click on it to create an OnTimer event handler. Modify it som it looks something like this:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
    Label3.Caption := TimeToStr(Time)
end;

And there you have it...

anon
Nice answer, you beat me too it ;o)
stukelly
Note that this won't update if the main thread is busy - e.g. loading or saving data, unless there is a call to Application.ProcessMessages as the windows timer message won't be processed.
Gerry
@Gerry: True. But since using the VCL from multiple threads is out of the question, without Application.ProcessMessages() there is no way *at* *all* to keep anything in the GUI updated during long-running tasks.
mghie
+2  A: 

So drop a timer on the form, set to tick every 500ms or so, and write the time to your text time field in the event handler for the timer. You need to tick faster than smallest time interval you want to display. Note also that windows timers are pretty low priority, so if the CPU gets loaded, your timer might seem to "stick".

Bob Moore
+5  A: 

You can use the Timer component to update the label caption every second.

  1. Drop a timer on your form.
  2. Set the Interval property to 500, which will update the caption every half second.
  3. Set the Enabled property to true.
  4. Double click on the timer and add the following code.

    Label3.Caption := TimeToStr(Time)

You can also use the FormatDateTime function, to change how time is displayed

stukelly
A: 

mh but why CAN i delete some of my questions? this one for example isnt deletable, but others i didnt need anymore, i deleted.

Alex
You don't delete questions on SO, since one of its goals is to create a searchable knowledge base for programmers.
mghie