Solved

simple chronometer component

Posted on 2002-03-12
6
562 Views
Last Modified: 2010-04-05
Hi guys,

I am looking for a simple chronometer component thats count from N seconds to zero. I found some components on the net but no one that runs backwards. Can anybody write a simple chronometer component. Minimal functionality: stop, pause, reset, running backward or forward...

thanks in advance...
Manuel Lopez (lopem)
0
Comment
Question by:lopem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6859656
I am not sure overall what you are asking but for simple backwards couting using a set time I do this whay.

Lets say we want to count seconds and my statrting point is 10 and I want to count it down to 0. What I do is to setup a Timer (disabled) component with an interval of 1000 miliseconds. Now I declare a variable global to my unit and call it maybe iCountDown. Now I set the variable to

iCountDown := 10;

Now I enable the timer and on each cycle I Dec(iCountDown) until it hits 0.

Keep in mind this is a simple approach and not entirely precise in time measurement.

Something like this

var
  iCountDown: Integer;

procedure TForm1.Timer1Timer(Sender: TObject);
begin

  Dec(iCountDown);
  Label1.Caption := IntToStr(iCountDown);
  if iCountDown <= 0 then Timer1.Enabled := False;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  iCountDown := 10;
  Label1.Caption := IntToStr(iCountDown);
  Timer1.Enabled := True;

end;


The Crazy One
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 6859779
I once wrote a little component which counts backwards.
This component had 1 event, OnCountFinished that was triggered the moment the counter reaches 0.

Maybe I can adapt it a little so that I can transform it into a chronometer.

Give me your email address and I'll send you this component if it is ready (Could take a while, because I'm pretty busy right now).

Best regards,
The Mayor.
0
 
LVL 3

Author Comment

by:lopem
ID: 6859786
morsa@la-morsa.com

I would really appreciate your effort... I will add 50 more points...

thanks in advance!

best regards
Manuel Lopez (lopem)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 6859836
An extra 50 points ??

I start coding right away now ;-)
0
 
LVL 6

Accepted Solution

by:
wimmeyvaert earned 150 total points
ID: 6862530
Hi lopem,

Just sent you the chrono-component.

Hope it's of some use.

Let me know if it works, and what can be changed in order to improve it for your own needs !!!

The Mayor.
I also post the code. Every remark is welcome (I'm just a little amateur that writes a little component from time to time) :

unit Comp_ChronoMeter;

interface

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

type
  TCountMode = (cmForward, cmBackward);  { Own created type }

  { The Component }
  TChronoMeter = class(TPanel)
  private
    { Private declarations }
    intCount   : Integer;                    { To keep the current count-position. }
    pnlChrono       : TPanel;         { Panel inside component to show current pos. }
    tmrChrono       : TTimer;         { Timer linked to the component to do counting . }
    sbtnStart       : TspeedButton;   { Start-button for chrono }
    sbtnPause       : TspeedButton;   { Pause-button for chrono }
    sbtnReset       : TspeedButton;   { Reset-button for chrono }
    FCountMode : TCountMode;
    FCountStart: Integer;
    FCountStop : Integer;
    FCountInterval: Integer;          { Count speed in ms. (1000 = update counter every second. }
    FOnCountFinished: TNotifyEvent;   { Event when counting has finished. }
    procedure UpdateChrono(Sender: TObject);
    procedure StartChrono(Sender: TObject);
    procedure PauseChrono(Sender: TObject);
    procedure ResetChrono(Sender: TObject);
    procedure SetCountMode(const Value: TCountMode);
    procedure SetCountStart(const Value: Integer);
    procedure SetCountStop(const Value: Integer);
    procedure SetCountInterval(const Value: Integer);
  protected
    { Protected declarations }
    procedure DoCountFinished( Sender: TObject );
  public
    { Public declarations }
    Constructor Create( AOwner: Tcomponent); override;
    Destructor  Destroy; override;
    procedure       Loaded; override;
  published
    { Published declarations }
    Property CountMode              : TCountMode read FCountMode write SetCountMode;
    Property CountStart             : Integer read FCountStart write SetCountStart;
    Property CountStop              : Integer read FCountStop write SetCountStop;
    Property CountInterval       : Integer read FCountInterval write SetCountInterval;

    property OnCountFinished: TNotifyEvent  read FOnCountFinished write FOnCountFinished;
end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('MyComps', [TChronoMeter]);
end;

{ TChronoMeter }


{ TChronoMeter }

constructor TChronoMeter.Create(AOwner: Tcomponent);
begin
  inherited;
  { Set some default values for component }
  Caption                := '';
      BevelInner          := bvNone;
  BevelOuter          := bvNone;
  Height                     := 22;
  Width                           := 200;

  { Create the inside panel that holds the current count-position }
      pnlChrono := TPanel.Create( Self );
      With pnlChrono do
  begin
        Align                       := alLeft;
        BevelInner           := bvLowered;
        BevelOuter           := bvRaised;
    Caption                  := IntToStr(intCount);
    Width                            := 70;
            Parent                       := Self;
  end;

  { Create the linked timer and set some properties }
  tmrChrono := TTimer.Create( Self );
      With tmrChrono do
  begin
    Enabled   := False;
    Interval       := 100;
    OnTimer       := UpdateChrono;
  end;

  { Create the start chrono button and set some properties }
  sbtnStart := TspeedButton.Create( Self );
  With sbtnStart do
  begin
    Parent  := Self;
        Caption := 'Start';
    Top                        := 0;
    Left                  := 75;
    Width                  := 35;
    Height            := 22;
    Flat                  := True;
    OnClick       := StartChrono;
  end;

  { Create the pause chrono button and set some properties }
  sbtnPause := TspeedButton.Create( Self );
  With sbtnPause do
  begin
    Parent              := Self;
        Caption             := 'Pause';
    Top                              := 0;
    Left                        := 115;
    Width                        := 35;
    Height                  := 22;
    Flat                        := True;
    AllowAllUp       := True;
    GroupIndex       := 1;
    Enabled             := False;
    OnClick             := PauseChrono;
  end;

  { Create the reset chrono button and set some properties }
  sbtnReset := TspeedButton.Create( Self );
  With sbtnReset do
  begin
    Parent  := Self;
        Caption := 'Reset';
    Top                        := 0;
    Left                  := 155;
    Width                  := 35;
    Height            := 22;
    Flat                  := True;
    OnClick       := ResetChrono;
  end;

  { Set some properties of the component after its linked components are created. }
      CountInterval := 1000;
  CountMode     := cmForward;
  CountStart    := 0;
  CountStop     := 60;
end;


procedure TChronoMeter.Loaded;
begin
  inherited;
      { Make sure the Caption of the Chrono (which is a simple panel) is blanked out at runtime }
      Self.Caption := '';
end;


destructor TChronoMeter.Destroy;
begin
  { Make sure to manually destroy every sel-created component. }
  FreeAndNil(pnlChrono);
  FreeAndNil(tmrChrono);
  FreeAndNil(sbtnStart);
  FreeAndNil(sbtnPause);
  FreeAndNil(sbtnReset);
  inherited;
end;


procedure TChronoMeter.UpdateChrono(Sender: TObject);
begin
  { The Code should only be executed at runtime, not at designtime. }
  if not (csDesigning in Self.ComponentState) then
  begin
    tmrChrono.Enabled  := False;
    if FCountMode = cmForward then
              Inc(intCount)
    else
              Dec(intCount);
    pnlChrono.Caption  := IntToStr(intCount);
    pnlChrono.Refresh;
    tmrChrono.Enabled  := True;
    { Trigger OnCountFinished-Event when counting has reached end. }
    if FCountMode = cmForward then
    begin
              if intCount >= FCountStop then DoCountFinished(Sender);
    end
    else
    begin
              if intCount <= FCountStop then DoCountFinished(Sender);
    end;
  end;
end;


procedure TChronoMeter.StartChrono(Sender: TObject);
begin
  sbtnStart.Enabled := False;
  sbtnPause.Enabled := True;
      tmrChrono.Enabled := True;
end;


procedure TChronoMeter.PauseChrono(Sender: TObject);
begin
      tmrChrono.Enabled := not sbtnPause.Down;
end;


procedure TChronoMeter.ResetChrono(Sender: TObject);
begin
      tmrChrono.Enabled := False;
  intCount                              := 0;
  pnlChrono.Caption      := IntToStr(intCount);
  sbtnStart.Enabled := True;
  sbtnPause.Enabled := False;
end;


procedure TChronoMeter.SetCountMode(const Value: TCountMode);
begin
  if FCountMode <> Value then
        FCountMode := Value;
end;

procedure TChronoMeter.SetCountStart(const Value: Integer);
begin
  if FCountStart <> Value then
  begin
        FCountStart       := Value;
        intCount                  := Value;
    pnlChrono.Caption  := IntToStr(intCount);
  end
end;

procedure TChronoMeter.SetCountStop(const Value: Integer);
begin
  if FCountStop <> Value then
        FCountStop := Value;
end;

procedure TChronoMeter.SetCountInterval(const Value: Integer);
begin
  if FCountInterval <> Value then
  begin
        FCountInterval := Value;
            tmrChrono.Interval := Value;
  end;
end;


procedure TChronoMeter.DoCountFinished(Sender: TObject);
begin
      { Code that is alway executed when the OnCountFinished-Event is triggered. }
      //PauseChrono(Sender);
      ResetChrono(Sender);
  if Assigned(FOnCountFinished) then
    FOnCountFinished(Self);
end;

end.
0
 
LVL 3

Author Comment

by:lopem
ID: 6862771
Thanks buddy...

I will check it tonight... In the meantime thanks again in advance.

best regards
manuel lopez (lopem)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question