Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > VCL

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 05-03-2011, 09:19 AM
joostha joostha is offline
Junior Member
 
Join Date: May 2011
Posts: 1
Post Making a Component from a non descendent of TComponent?

Dear Delphipages readers,

I am new to the usage of Components in Delphi and have been investigating what they can do for me. I use Delphi for programming discrete time simulations with the usage of Tomas: http://www.tomasweb.com/.

Tomas uses tomaselements (in a nonvisual run) / tomasshapes (visual runs) that have certain properties with useful methods in simulation. The package is normally used for transportation issues f.e. aiports, container terminals etc.

To get acquainted with the usage of components I followed the following tutorials from about.com:

Creating delphi components, part 1-3
http://delphi.about.com/library/bluc/text/uc080701a.htm
http://delphi.about.com/library/bluc/text/uc083101a.htm
http://delphi.about.com/library/bluc/text/uc092501a.htm

and
Creating Custom Delphi Components - Inside and Out
http://delphi.about.com/library/bluc/text/uc061102a.htm


In tomas oftentimes I have to create certain vehicles: Automatic Guided Vehicles, Cars, Busses, etc. etc. What I would like to do is make a toolbar with some standard Vehiclecomponents so i can add them to my program. In developer mode, in the object inspector I want to make certain variables available like speed/arc/fuelusage/etc.etc.


If I look at how tomaselements and Tcomponents descent, I see that tomaselements do NOT descent from TObjects but from TPersistent.

Problem:
Is there any way to write a component that has all the properties of a tomaselement but can be used as a component?

I have tried to fiddle around with this but I believe it is necessary for a Component to descent from a TComponent?

I hope my question is clear, if not, don't hesitate to ask for an elaboration!

With kind regards!

Joost



Reply With Quote
  #2  
Old 05-04-2011, 12:23 PM
vcldeveloper vcldeveloper is offline
Member
 
Join Date: Jan 2011
Location: Iran
Posts: 80
Default

Hi,

You should write a wrapper component for them, that means, you should create a class descendant from TComponent which contains any of Tomas classes as its fields. If you want the component to act just similar to Tomas classes, you gotta create a similar interface for your component, and map each property and method to the underlying field. Here is an example:

/// Interface section:

Code:
  TMyPersistentClass = class(TPersistent)
  private
    FMyField : Integer;
  public
    procedure DoSomething;
  published
    property  MyProp: Integer read FMyField write FMyField;
  end;


  TMyWrapperComponent = class(TComponent)
  private
    FMyPersistentClass : TMyPersistentClass;
    function GetMyProp: Integer;
    procedure SetMyProp(Value: Integer);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure DoSomething;
  published
    property MyProp: Integer read GetMyProp write SetMyProp;
  end;


/// Implementation:
Code:
constructor TMyWrapperComponent.Create(AOwner: TComponent);
begin
  inherited;
  FMyPersistentClass := TMyPersistentClass.Create;
end;

destructor TMyWrapperComponent.Destroy;
begin
  FMyPersistentClass.Free;
  inherited;
end;

procedure TMyWrapperComponent.DoSomething;
begin
  FMyPersistentClass.DoSomething;
end;

function TMyWrapperComponent.GetMyProp: Integer;
begin
  Result := FMyPersistentClass.MyProp;
end;

procedure TMyWrapperComponent.SetMyProp(Value: Integer);
begin
  FMyPersistentClass.MyProp := Value;
end;
Reply With Quote
Reply

Tags
components, tcomponent, tomas

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:26 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.