From Clomosy Docs

No edit summary
No edit summary
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
If there is any code or event that you want to run at certain time intervals or within a certain period of time, the component we need to do this desired job is the TClTimer component.<br>
<div class="alert alert-ligth border border-3 border-primary-subtle rounded-5 p-4 shadow-sm" role="alert">
If we have code that we want to run every second, we write it to the tbeOnTimer event of the TClTimer component. Then, if we set the component's Interval property to 1000, the code we wrote will be executed every second.<br>
function AddNewTimer(AComponent: TCLComponent; xName: string; xInterval:Integer): TClTimer;
</div>


AddNewTimer(TComponent, xName, xMillisecond): TClTimer
<span style="color:blue"><b>AComponent</b></span> : Specifies the parent of the object to be defined.<br>


<span style="color:blue"> TComponent</span> : The variable name of the defined component is written. Here you should write the component variable name of whatever your component will be in.
<span style="color:blue"><b>xName</b></span> : The name of the defined timer should be written.<br>


<span style="color:blue"> xName</span> : The name of the defined timer should be written.
<span style="color:blue"><b>xInterval</b></span> : Type the trigger time of the time in milliseconds. If we write the time as 0 here, the trigger will not occur at all.<br>


<span style="color:blue"> xMillisecond</span> : Type the trigger time of the time in milliseconds. If we write the time as 0 here, the trigger will not occur at all.


Let's create a TclTimer.<br>
If there is any code or event that you want to run at certain time intervals or within a certain period of time, the component we need to do this desired job is the TClTimer component.<br>
1. Create a new project.<br>
If there is code that needs to be executed based on time intervals, it should be written in the tbeOnTimer event of the TClTimer component. Then, by setting the Interval property of the component in milliseconds, the written code will be executed at the desired time intervals.<br>
 
2. You need to define TclTimer on the form. To do this, you should add under the var parameter on the ide as follows. It is the name of your variable you typed at the beginning. You should define this as you want and add it as TclTimer.<br>
 
'''var'''
GetTimer: TClTimer;
 
3. Add the TClTimer to the form. For this, you must add the begin end block and add it inside the form after the form is defined. By saying MyForm.AddNewTimer, we actually add to the form we have defined. Here, you need to add your form definition as whatever you wrote it.
GetTimer:= MyForm.'''AddNewTimer'''(MyForm,'GetTimer',1000); // the action is triggered every 1 second.
 
4. While defining the component, you can define it manually by typing. Another method is to write its shortcut. If you type "AddNewTimer" while the shortcut is in the code block, a pop-up menu will appear.<br>
 
[[File:TclTimerShortcut.png|frameles|400px]]<br><br>
 
As soon as you click, the following block will come automatically.<br>
AddNewTimer(xOwner:TComponent; xName:String; xInterval:Integer);
 
5. We gave the variable name while defining the TClTimer in var. Now when you add this in begin end you should use this variable name in all definitions. Your code will throw an error when you write these variable names incorrectly.
 
6. Now, if we want to activate the click feature of our TCLTimer component, we can do it with AddNewEvent. Here we should write tbeOnTimer as the 2nd parameter. In the last parameter, we should write the action that will be triggered when clicked.<br>
 
MyForm.AddNewEvent(GetTimer,'''tbeOnTimer''','timerShow');
 
7. Below, the time to be triggered is written in the 3rd parameter at the time of calling with the AddNewTimer function. If we write the time as 0 here, the trigger will not occur at all. In this case, a trigger time can be given to the Interval property for triggering. Thus, even though it is 0, triggering with interval will still be provided.<br>
 
'''Interval Property:''' This property specifies the intervals at which the timer component will be run. If the Interval is set to 1000, the timer will run every second. So 1000 means 1 second. If the Interval value is 500, the timer will run 2 times per second.<br>
 
The following code show how to define the interval property of the timer component.<br>
 
VariableValue.Interval := 1000;


8. '''Enabled Property:''' It is the property that the TClTimer component decides whether to run the codes or not. If the enabled property is true, the timer runs the codes at the desired times. If the Enabled property is false, the timer will not work.<br>
<div class="table-responsive">
{| class="wikitable" style="border: 2px solid #c3d7e0"
! style="background-color: #c3d7e0"| Feature  !!style="background-color: #c3d7e0"| Use of !!style="background-color: #c3d7e0"| Definition
|-
|TClTimer || Timer1: TclTimer; || A variable belonging to the TclTimer class is created.
|-
|AddNewTimer || Timer1 = Form1.AddNewTimer(Form1,'Timer1',1000); // the action is triggered every 1 second. || A new TclTimer is added to the form.
|-
|Interval ||Timer1.Interval = 1000; || When creating an object, the time interval to be triggered is specified in the third parameter of the AddNewTimer function. If we set the time to 0 here, the trigger will never occur. In this case, a trigger time can be assigned to the Interval property to enable the triggering. Thus, even if it's set to 0, the triggering will still occur with the interval.<br>
This property specifies the intervals at which the timer component will operate. If the interval is set to 1000, the timer will run every second, meaning 1000 represents 1 second. If the interval value is 500, the timer will run twice per second.
|-
|Enabled || Timer1.Enabled = True; //or False || It is the property that determines whether the TClTimer component will run or not. If the Enabled property is set to true, the timer executes its code at the specified intervals. If the Enabled property is set to false, the timer does not run.
|-
|tbeOnTimer ||Form1.AddNewEvent(Timer1,tbeOnTimer,'timerShow'); //timerShow : Void || If we want to activate the click event of our TCLTimer component, it is done using AddNewEvent. Here, tbeOnTimer is written as the second parameter. In the last parameter, the action to be triggered on click is specified.
|}
</div>


In the codes below, the enabled property of the timer component is changed to true and false.<br>
<b>Example</b><br>
In the example, a stopwatch is created using a timer. The timer can be started and stopped with two buttons.<br>


VariableValue.Enabled := True;
<pre>
VariableValue.Enabled := False;
var
  MyForm:TclForm;
  GetTimer: TClTimer;
  GetStartTimerBtn,GetStopTimerBtn : TClProButton;
  duration, sn ,ss : Integer;
  lblTimer : TclLabel;
  lytTimer :TClLayout;
  pnlTimer :TClPanel;


void BtnStartClick;
{
  //GetTimer.Interval = 100; //You can enable it if you don't use a time trigger when calling AddNewTimer.
  GetTimer.Enabled = True;
  MyForm.AddNewEvent(GetTimer,tbeOnTimer,'timerShow');
}


9. To tell you the truth, you have created a simple application that does nothing yet. Let's save and start using our project. You can save in one of two ways:<br>
void BtnStopClick;
Click the save icon (the button in the upper right corner) or press Ctrl + S to save the project and see what you've done on the platforms now.
{
  GetTimer.Enabled = False;
}


'''Example:'''<br>
void timerShow;
In the example, the chromometer was built using a timer. You can use it this way or you can use this component for any scheduling you want.<br>
{
  Inc(duration);
  sn = (duration div 10);
  ss = (duration mod 10);
  lblTimer.Caption = IntToStr(sn)+'.'+IntToStr(ss);
}


'''var'''
{
    MyForm:TclForm;
   duration = 0;
    GetTimer: TClTimer;
  MyForm = TclForm.Create(Self);
    GetStartTimerBtn,GetStopTimerBtn : TClProButton;
  GetTimer= MyForm.AddNewTimer(MyForm,'GetTimer',100);  
    duration, sn ,ss : Integer;
 
    lblTimer : TclLabel;
  pnlTimer=MyForm.AddNewPanel(MyForm,'pnlTimer');
    lytTimer :TClLayout;
  pnlTimer.Align=AlCenter;
    pnlTimer :TClPanel;<br> 
  pnlTimer.Height=150;
'''Procedure''' BtnStartClick;
  pnlTimer.Width=300;
'''begin'''
 
    //GetTimer.Interval := 100; //You can enable it if you don't use a time trigger when calling AddNewTimer.
  lytTimer = MyForm.AddNewLayout(pnlTimer,'lytTimer');
    GetTimer.Enabled := True;
  lytTimer.Align=ALTop;
    MyForm.AddNewEvent(GetTimer,tbeOnTimer,'timerShow');
  lytTimer.Height = 70;
   '''End;'''<br> 
  lytTimer.Width = 300;
'''procedure''' BtnStopClick;
 
'''begin'''
  GetStartTimerBtn = MyForm.AddNewProButton(lytTimer,'GetStartTimerBtn','Start');
    GetTimer.Enabled := False;
  GetStartTimerBtn.Align = alLeft;
'''end;'''<br> 
  GetStartTimerBtn.Width = 100;
'''procedure''' timerShow;
  GetStartTimerBtn.Height = 40;
'''begin'''
  GetStartTimerBtn.clProSettings.BorderColor = clAlphaColor.clHexToColor('#7A3E65');
    Inc(duration);
  GetStartTimerBtn.clProSettings.RoundHeight = 2;
    sn := (duration div 10);
  GetStartTimerBtn.clProSettings.RoundWidth = 2;
    ss := (duration mod 10);
  GetStartTimerBtn.clProSettings.BorderWidth = 3;
    lblTimer.Caption := IntToStr(sn)+'.'+IntToStr(ss);
  GetStartTimerBtn.clProSettings.IsRound = True;
'''end;'''<br>
  GetStartTimerBtn.SetclProSettings(GetStartTimerBtn.clProSettings);
'''begin'''
  MyForm.AddNewEvent(GetStartTimerBtn,tbeOnClick,'BtnStartClick');
    duration := 0;
 
    MyForm := TclForm.Create(Self);
 
    GetTimer:= MyForm.AddNewTimer(MyForm,'GetTimer',100); <br>
  GetStopTimerBtn = MyForm.AddNewProButton(lytTimer,'GetStopTimerBtn','Stop');
    pnlTimer:=MyForm.AddNewPanel(MyForm,'pnlTimer',<nowiki>''</nowiki>);
  GetStopTimerBtn.Align = alRight;
    pnlTimer.Align:=AlCenter;
  GetStopTimerBtn.Width = 100;
    pnlTimer.Height:=150;
  GetStopTimerBtn.Height = 40;
    pnlTimer.Width:=300;<br>
  GetStopTimerBtn.SetclProSettings(GetStartTimerBtn.clProSettings);
    lytTimer := MyForm.AddNewLayout(pnlTimer,'lytTimer');
  MyForm.AddNewEvent(GetStopTimerBtn,tbeOnClick,'BtnStopClick');
    lytTimer.Align:=ALTop;
 
    lytTimer.Height := 70;
 
    lytTimer.Width := 300;<br>
  lblTimer= MyForm.AddNewLabel(pnlTimer,'lblTimer','0.0');
    GetStartTimerBtn := MyForm.AddNewProButton(lytTimer,'GetStartTimerBtn',<nowiki>''</nowiki>);
  lblTimer.StyledSettings = ssFamily;
    clComponent.SetupComponent(GetStartTimerBtn,'{"caption":"Start","Align" : "Left",
  lblTimer.TextSettings.Font.Size=25;
    "Width" :100,"Height":40,"RoundHeight":2, "RoundWidth":2,"BorderColor":"#7A3E65","BorderWidth":2}');
  lblTimer.Align = alBottom;
    MyForm.AddNewEvent(GetStartTimerBtn,tbeOnClick,'BtnStartClick');<br>
  lblTimer.Margins.Left= 130;
    GetStopTimerBtn := MyForm.AddNewProButton(lytTimer,'GetStopTimerBtn',<nowiki>''</nowiki>);
  lblTimer.Height = 50;
    clComponent.SetupComponent(GetStopTimerBtn,'{"caption":"Stop","Align" : "Right",
  lblTimer.Width = 200;
    "Width" :100,"Height":40,"RoundHeight":2, "RoundWidth":2,"BorderColor":"#7A3E65","BorderWidth":2}');
 
    MyForm.AddNewEvent(GetStopTimerBtn,tbeOnClick,'BtnStopClick');<br>
  MyForm.Run;
    lblTimer:= MyForm.AddNewLabel(pnlTimer,'lblTimer',<nowiki>''</nowiki>);
}
    lblTimer.StyledSettings := ssFamily;
</pre>
    lblTimer.TextSettings.Font.Size:=25;
    lblTimer.Align := alBottom;
    lblTimer.Margins.Left:= 130;
    lblTimer.Height := 50;
    lblTimer.Width := 200;<br>
    MyForm.Run;
'''end;'''


'''Output:'''<br>
<b>Output:</b><br>
{{#ev:youtube| https://www.youtube.com/watch?v=gtI1IjYliws}}
{{#ev:youtube| https://www.youtube.com/watch?v=gtI1IjYliws}}


= See Also =
<h2> See Also </h2>
* [[Components]]
* [[Object Properties]]
* [[Object Properties]]
* [[AddNewEvent]]
* [[AddNewEvent]]
{{#seo:|title=TClTimer Using in Clomosy - Clomosy Docs}}
{{#seo:|description=TClTimer enables executing code at specified intervals in Clomosy, with adjustable triggers, intervals, and events for timed actions in your applications.}}

Latest revision as of 14:02, 24 December 2024

AComponent : Specifies the parent of the object to be defined.

xName : The name of the defined timer should be written.

xInterval : Type the trigger time of the time in milliseconds. If we write the time as 0 here, the trigger will not occur at all.


If there is any code or event that you want to run at certain time intervals or within a certain period of time, the component we need to do this desired job is the TClTimer component.
If there is code that needs to be executed based on time intervals, it should be written in the tbeOnTimer event of the TClTimer component. Then, by setting the Interval property of the component in milliseconds, the written code will be executed at the desired time intervals.

Feature Use of Definition
TClTimer Timer1: TclTimer; A variable belonging to the TclTimer class is created.
AddNewTimer Timer1 = Form1.AddNewTimer(Form1,'Timer1',1000); // the action is triggered every 1 second. A new TclTimer is added to the form.
Interval Timer1.Interval = 1000; When creating an object, the time interval to be triggered is specified in the third parameter of the AddNewTimer function. If we set the time to 0 here, the trigger will never occur. In this case, a trigger time can be assigned to the Interval property to enable the triggering. Thus, even if it's set to 0, the triggering will still occur with the interval.

This property specifies the intervals at which the timer component will operate. If the interval is set to 1000, the timer will run every second, meaning 1000 represents 1 second. If the interval value is 500, the timer will run twice per second.

Enabled Timer1.Enabled = True; //or False It is the property that determines whether the TClTimer component will run or not. If the Enabled property is set to true, the timer executes its code at the specified intervals. If the Enabled property is set to false, the timer does not run.
tbeOnTimer Form1.AddNewEvent(Timer1,tbeOnTimer,'timerShow'); //timerShow : Void If we want to activate the click event of our TCLTimer component, it is done using AddNewEvent. Here, tbeOnTimer is written as the second parameter. In the last parameter, the action to be triggered on click is specified.

Example
In the example, a stopwatch is created using a timer. The timer can be started and stopped with two buttons.

var
  MyForm:TclForm;
  GetTimer: TClTimer;
  GetStartTimerBtn,GetStopTimerBtn : TClProButton;
  duration, sn ,ss : Integer;
  lblTimer : TclLabel;
  lytTimer :TClLayout;
  pnlTimer :TClPanel;

void BtnStartClick;
{
  //GetTimer.Interval = 100; //You can enable it if you don't use a time trigger when calling AddNewTimer.
  GetTimer.Enabled = True;
  MyForm.AddNewEvent(GetTimer,tbeOnTimer,'timerShow');
}

void BtnStopClick;
{
  GetTimer.Enabled = False;
}

void timerShow;
{
  Inc(duration);
  sn = (duration div 10);
  ss = (duration mod 10);
  lblTimer.Caption = IntToStr(sn)+'.'+IntToStr(ss);
}

{
  duration = 0;
  MyForm = TclForm.Create(Self);
  GetTimer= MyForm.AddNewTimer(MyForm,'GetTimer',100); 
  
  pnlTimer=MyForm.AddNewPanel(MyForm,'pnlTimer');
  pnlTimer.Align=AlCenter;
  pnlTimer.Height=150;
  pnlTimer.Width=300;
  
  lytTimer = MyForm.AddNewLayout(pnlTimer,'lytTimer');
  lytTimer.Align=ALTop;
  lytTimer.Height = 70;
  lytTimer.Width = 300;
  
  GetStartTimerBtn = MyForm.AddNewProButton(lytTimer,'GetStartTimerBtn','Start');
  GetStartTimerBtn.Align = alLeft;
  GetStartTimerBtn.Width = 100;
  GetStartTimerBtn.Height = 40;
  GetStartTimerBtn.clProSettings.BorderColor = clAlphaColor.clHexToColor('#7A3E65');
  GetStartTimerBtn.clProSettings.RoundHeight = 2;
  GetStartTimerBtn.clProSettings.RoundWidth = 2;
  GetStartTimerBtn.clProSettings.BorderWidth = 3;
  GetStartTimerBtn.clProSettings.IsRound = True;
  GetStartTimerBtn.SetclProSettings(GetStartTimerBtn.clProSettings);
  MyForm.AddNewEvent(GetStartTimerBtn,tbeOnClick,'BtnStartClick');
  
  
  GetStopTimerBtn = MyForm.AddNewProButton(lytTimer,'GetStopTimerBtn','Stop');
  GetStopTimerBtn.Align = alRight;
  GetStopTimerBtn.Width = 100;
  GetStopTimerBtn.Height = 40;
  GetStopTimerBtn.SetclProSettings(GetStartTimerBtn.clProSettings);
  MyForm.AddNewEvent(GetStopTimerBtn,tbeOnClick,'BtnStopClick');
  
  
  lblTimer= MyForm.AddNewLabel(pnlTimer,'lblTimer','0.0');
  lblTimer.StyledSettings = ssFamily;
  lblTimer.TextSettings.Font.Size=25;
  lblTimer.Align = alBottom;
  lblTimer.Margins.Left= 130;
  lblTimer.Height = 50;
  lblTimer.Width = 200;
  
  MyForm.Run;
}

Output:

See Also