Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2003-10-22
5
Medium Priority
?
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 336 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 332 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 23

Assisted Solution

by:Ferruccio Accalai
Ferruccio Accalai earned 332 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 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