Solved

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

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

Independent Software Vendors: 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

728 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