Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number of times a TButton was clicked 1

Status
Not open for further replies.

Mezzzo

Technical User
May 30, 2003
56
US
I did a search but no luck. How do you count the
number of times a TButton was clicked.

Thanks
 
Increment a counter in the button's OnClick event and add an application.processmessages for good measure.

 
If you've not tried writing a component before then this would be a simple example to begin with.

Call your new component TNewButton. TNewButton will be identical to TButton but with some extra code. We can inherit all the behaviour of TButton and add our own extra code to count the clicks.
Code:
type
  TNewButton = class(TButton)
  private
  public
  end;
The above code would create a class TNewButton which was identical to TButton. Now to add our new functionality. First, we want a private variable to keep count of the number of clicks. By convention, private variables begin with the letter 'f'. It is also good practice to access private variables by using properties.
Code:
type
  TNewButton = class(TButton)
  private
    fSingleClickCount: integer;
  public
    property SingleClickCount: integer 
      read fSingleClickCount
      write fSingleClickCount;
  end;
Now we need to replace the OnClick event handler with one of our own which will count the clicks. Obviously we want this event handler to also do everything that the TButton OnClick event handler does so we ask it to inherit the TButton OnClick code.
Code:
type
  TNewButton = class(TButton)
  private
    fSingleClickCount: integer;
  public
    procedure Click; override;
    property SingleClickCount: integer
      read fSingleClickCount
      write fSingleClickCount;
  end;
The actual event handler looks like this:
Code:
procedure TNewButton.Click;
begin
  inc ( fSingleClickCount );
  inherited Click;
end;
Finally we need to register the component with Delphi so that it appears in the component palette. I've chosen to put it in the Samples tab.
Code:
unit uNewButton;

interface

uses StdCtrls, Classes;

type
  TNewButton = class(TButton)
  private
    fSingleClickCount: integer;
  public
    procedure Click; override;
    property SingleClickCount: integer
      read fSingleClickCount
      write fSingleClickCount;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TNewButton]);
end;

procedure TNewButton.Click;
begin
  inc ( fSingleClickCount );
  inherited Click;
end;

end.
Save this file. I've called it uNewButton.pas and install the component by clicking on Delphi's Component | Install Component... menu item.

You should now be able to create an application that uses the TNewButton component. If you want to know the number of times your NewButton has been clicked you access the SingleClickCount property e.g.
Code:
  ShowMessage ( IntToStr(NewButton1.SingleClickCount) );
This is a rather simple explanation of a simple component but it might get you started on developing components which I find a fun and rewarding activity. The ability to create one's own components relatively simply is one of the great strengths of Delphi.

Andrew
Hampshire, UK
 
Hi Towerbase

That was a great answer.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top