Solved

Writing code refering to a Ttable on a form from a custom component

Posted on 2003-10-22
5
190 Views
Last Modified: 2010-04-05
I am wondering if it is posible to write code refering to a specific Ttable on a form in a custom component.  I am trying to write code on the onClick and onContextPopup event of a customised TLabel and TImage so a user can access a database from a dynamically created Image and label that have being created on the OnMouseUp event of the form.  
I am using delphi 5.
Hope this makes sence
Thanks

Graeme
0
Comment
Question by:KiwiGraeme
5 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 84 total points
ID: 9597384
?

....
procedure TForm1.MyPredefinedOnClick(Sender : TObject);
begin
  //whatever needed here
end;

procedure TForm1.MyPredefinedOnContextPopUp(Sender : TObject; MousePos: TPoint;
  var Handled: Boolean););
begin
  //whatever needed here
end;


....
Procedure TForm1.CreateLabel;
begin
  with Tlabel.Create(self) do
  begin
    top := WhatEver;
    left := WhatEver;
    parent := self;
    OnClick := MyPredefinedOnClick;
    OnContextPopUp := MyPredefinedOnContextPopUp;
    //...maybe more
  end;
end;
...


meikl ;-)

0
 
LVL 6

Assisted Solution

by:swift99
swift99 earned 83 total points
ID: 9597446
It is generally bad to break encapsulation.

If you really need to talk to the TTable component then ensure that the table is exposed as a property on the custom component.  From there on it's just like any other variable.

What you are suggesting is very straightforward.  meikl gave you most of what you need.
0
 
LVL 22

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 83 total points
ID: 9597928
BTW, if you're going to create these cmp dinamically maybe you need some differents procedures for every one...
And also you can have more than a Dataset on your form that must be linked to the cmps...

So an example to code these customized components should be this:

unit MyLabelAndImage;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, StdCtrls,db,extctrls;

type
  TLabel1 = class(TLabel)
  private
  fDataset: TDataset; //so you can use all TDataset descendents (ADO, IB, BDE...)
    { Private declarations }
  protected
    { Protected declarations }
    procedure Click; Override;
  public
    { Public declarations }
  published
    { Published declarations }
    property Dataset: TDataset read fDataset write fDataset;
  end;
  TImage1 = class(TImage)
  private
  fDataset: TDataset;
 
    { Private declarations }
  protected
    { Protected declarations }
    procedure DoContextPopup(MousePos: TPoint; var Handled: Boolean); override;
   
  public
    { Public declarations }
  published
    { Published declarations }
    property Dataset: TDataset read fDataset write fDataset;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Standard', [TLabel1,TImage1]);
end;
procedure TLabel1.Click;
begin
      inherited;
      if Assigned(fDataset) then
         caption := fDataset.Name;//just an example
   
end;
procedure TIMAGE1.DoContextPopup(MousePos: TPoint; var Handled: Boolean);
begin
      Inherited;
   if Assigned(fDataset) then
         Messagebox(0,PCHAR(Fdataset.name),PCHAR('Info'),0);//just an example
end;    
end.

F68 ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now