Tracer
Tracer

Reputation: 2736

Should I write components in Delphi instead of C++ Builder? How do I add events to a component?

I use C++ Builder (XE2) and I would need to develop some VCL components that would also be used in Delphi. As I understand C++ Builder supports Delphi code and Delphi components but not the other way around? If so, it would be better to start writing it in Delphi so that I don't do a double job?

Second part of my question is more technical; I know how to add a property in a VCL component but don't know how to add events. Could someone give me an example please (no matter Delphi or C++ Builder).

Thanks.

Upvotes: 2

Views: 947

Answers (2)

Arioch 'The
Arioch 'The

Reputation: 16045

As I understand C++ Builder supports Delphi code and Delphi components but not the other way around?

On source level - yes. But if you choose to distribute your library sourceless - BPL+DCP+DCU - then it would not matter, except for maybe some small incompatibilities, like Delphi lacking [] operator and C++ lacking virtual overloaded constructors.

Turns out this estimation was wrong. Read Remy's comment below


Most close to you example ov events is the VCL itself, sources are usually shipped with Delphi. If you have Delphi Starter/Trial without VCL sources - then get any opensource VCL library or component. Such as JediVCL or basically almost ANY VCL component with sources. For example any "FWS" (Free with sources) component 99% uses events.

Most basic and widely used event notifications type - such as TButton.OnClick, TForm.OnCreate and a lot of - is TNotifyEvent

Open Delphi Help for that type. Scroll to "See also" and see two links there.

Upvotes: 4

BugFinder
BugFinder

Reputation: 17858

Such as: (borrowed code from about.delphi.com)

type
  TState = (stStarted, stStopped);
  TStateChangeEvent = procedure
  (Sender : TObject; State : TState) of object;

  TThirdComponent = class(TSecondComponent) // or whatever
  private
    { Private declarations }
    FState  : TState;
    FOnStart,
    FOnStop  : TNotifyEvent;
    FOnStateChange  : TStateChangeEvent;
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
    procedure Start; override;
    procedure Stop; override;
    property State : TState
      read FState;
  published
    { Published declarations }
    property OnStart : TNotifyEvent
      read FOnStart
      write FOnStart;
    property OnStateChange : TStateChangeEvent
      read FOnStateChange
      write FOnStateChange;
    property OnStop : TNotifyEvent
      read FOnStop
      write FOnStop;
  end

Then you can do

procedure TThirdComponent.Start;
begin
  inherited; 
  FState := stStarted;
  if Assigned(OnStart) then OnStart(Self);
  if Assigned(OnStateChange) then 
    OnStateChange(Self, State);
end;

Upvotes: 2

Related Questions