To create a TTimer component in Delphi at runtime, you can use the TTimer class and the Create method of the component’s parent container.
Here is an example of how you might create a TTimer component at runtime and add it to a form:
|
1 2 3 4 5 6 7 8 9 |
var Timer1: TTimer; begin Timer1 := TTimer.Create(Form1); Timer1.Parent := Form1; Timer1.Interval := 1000; // Set the timer interval to 1 second Timer1.OnTimer := Timer1Timer; // Set the OnTimer event handler Timer1.Enabled := True; // Enable the timer end; |
This code creates a TTimer component and sets its parent to be the form Form1. It then sets the Interval property to 1000 milliseconds (1 second) and the OnTimer event handler to a procedure named Timer1Timer. Finally, it enables the timer by setting the Enabled property to True.
You can then implement the Timer1Timer procedure to specify the code that should be executed when the timer expires. For example:
|
1 2 3 4 |
procedure TForm1.Timer1Timer(Sender: TObject); begin ShowMessage(‘Timer expired’); end; |
This code displays a message box when the timer expires.
You can customize the behavior of the TTimer component by setting its various properties, such as the Enabled property to control whether the timer is active, the Interval property to set the timer interval, and the OnTimer event handler to specify the code that should be executed when the timer expires.
Leave a Reply