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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I write a simple Delphi Component?

Component Writing

How do I write a simple Delphi Component?

by  towerbase  Posted    (Edited  )

I would like to write a component that is like a TButton but keeps a count of the number of times it has been clicked.

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:
[color blue]
type
  TNewButton = class(TButton)
  private
  public
  end;
[/color]
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:
[color blue]
type
  TNewButton = class(TButton)
  private
    fSingleClickCount: integer;
  public
    property SingleClickCount: integer 
      read fSingleClickCount
      write fSingleClickCount;
  end;
[/color]
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:
[color blue]
type
  TNewButton = class(TButton)
  private
    fSingleClickCount: integer;
  public
    procedure Click; override;
    property SingleClickCount: integer
      read fSingleClickCount
      write fSingleClickCount;
  end;
[/color]
The actual event handler looks like this:
Code:
[color blue]
procedure TNewButton.Click;
begin
  inc ( fSingleClickCount );
  inherited Click;
end;
[/color]
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:
[color blue]
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.
[/color]
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:
[color blue]
  ShowMessage ( IntToStr(NewButton1.SingleClickCount) );
[/color]
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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top