Solved

How to create a DATASOURCE property

Posted on 1997-06-03
7
271 Views
Last Modified: 2010-04-06
I want to know how to create a datasource property in an own written component and to access the datasource, that means how to get the data from ttable or tquery ?
0
Comment
Question by:angerer
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:icampbe1
ID: 1336774
Declare a PROPERTY  of type TDataSource in your component.  Make if reference a private field like FDataSource: TDataSource.  In the components constructor, create the DataSource object, and be sure to free it in the component's destructor.  You can then use the properties and methods of your DataDource property to do whatever you want.

0
 
LVL 1

Author Comment

by:angerer
ID: 1336775
I am sorry but this is the stand of information i had before too. The documentation on this problem is not very good (or i am blind) and therefore i do not know how to access the data with this FDataSource.
It would be wonderfull if you have some code example and can help me with that.

Anyway - thank you very much

DI Bernhard Angerer
0
 
LVL 2

Expert Comment

by:icampbe1
ID: 1336776
Yes, It Works!  I'm not sure what you don't understand.

TYPE
   TAngerer = CLASS( Whatever )
   PRIVATE
     FDSource: TDataSource;
   PUBLIC
     CONSTRUCTOR Create(...);    {<---THis is up to you}
     DESTRUCTOR Destroy;   Override;
     PROPERTY DataSource: TDataSource  Read FDSource;
   END;

IMPLEMENTATION

CONSTRUCTOR TAngerer.Create(...):
BEGIN
   FDSource := TDataSource.Create( Self );
END;

DESTRUCTOR TAngerer.Destroy;
BEGIN
   FDSource.Free;
END;

Now, use your object....

XX ;= TAngerer.Create(....);
XX.DataSource := SomeTTable;  {Or whatever}

I hope this is more clear!

Ian C.

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 1

Author Comment

by:angerer
ID: 1336777
Thank you very much for your efford but,
let us inherit TAngerer from TEdit and please show me how i can assign the Text property with let as say the NAME column from the table ANIMALS.DBF (example table from delphi). In the text field should than appear 'Angel Fish' when the cursor is standing on the first position.

Thanks
bernhard
0
 

Accepted Solution

by:
brittain earned 100 total points
ID: 1336778
I'll assume you're trying to create a data-aware control.  

If so, you should check the Delphi docs & source for TFieldDataLink.  This is the class which actually does the data access.  In your class, the Datasource property is just a string.

Example from the source (TDBEdit):

constructor TDBEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  inherited ReadOnly := True;
  ControlStyle := ControlStyle + [csReplicatable];
  FDataLink := TFieldDataLink.Create;
  FDataLink.Control := Self;
  FDataLink.OnDataChange := DataChange;
  FDataLink.OnEditingChange := EditingChange;
  FDataLink.OnUpdateData := UpdateData;
end;

function TDBEdit.GetDataSource: TDataSource;
begin
  Result := FDataLink.DataSource;
end;

procedure TDBEdit.SetDataSource(Value: TDataSource);
begin
  FDataLink.DataSource := Value;
  if Value <> nil then Value.FreeNotification(Self);
end;

Clearly there is other code you need.  But not much.  I recommend Ray Konopka's book on developing custom controls, but the docs have a partial example.

Scott



0
 
LVL 2

Expert Comment

by:icampbe1
ID: 1336779
I'm sorry, I thought you said that you wanted to use a datasource within a control and perhaps to access a ttable from there.  I didn't know that you wanted to know how to make a control data aware.

Ian C.

0
 
LVL 1

Author Comment

by:angerer
ID: 1336780
Thank you...
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 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