Solved

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

Posted on 2003-10-22
5
185 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

11 Experts available now in Live!

Get 1:1 Help Now