Obtaining information about embedded frame component and passing it to the parentform

Hi all,

I have created a parentform and dynamically embedded with a array of frames, each containing a panel with a TSpeedButton.

My parentform has a database for information exchange and retrieval.

How do I get the attribute of which TSpeedButton I have clicked on when testing and passing it to the parentform so that the correct database access according to the button clicked is retrieved?

Even though the frames are embedded in the parentform, they do not seem to be able to communicate bi-directionally.

I have tried :

procedure TTestForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);

begin

  if Sender is TTestForm then
    ShowMessage('TestForm')
  else if Sender is TSpeedButton then
    ShowMessage('SpeedButton');

////  if (Sender as TSpeedButton).HasParent = true then
////    ShowMessage('HasParent');
end;

Clicking on the parentform will pop up 'TestForm'; clicking on the embedded Frame TSpeedButton does not. Is there a way to get the handler of the clicked TSpeedButton and used it in the parentform?
jasongthAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

antonio_fortunyCommented:
Try this (it works with Delphi 5)

unit Trf2;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,Buttons, ExtCtrls, Dialogs;

type
  TOKBottomDlg = class(TForm)
    OKBtn: TButton;
    CancelBtn: TButton;
    Panel1: TPanel;
    SpeedButton1: TSpeedButton;
    procedure SpeedButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
  public
    { Public declarations }
  end;

var
  OKBottomDlg: TOKBottomDlg;

implementation

{$R *.DFM}

procedure TOKBottomDlg.FormCreate(Sender: TObject);
begin
// create instances
SpeedButton3 := TSpeedButton.Create(Self);
SpeedButton2 := TSpeedButton.Create(Self);
end;

procedure TOKBottomDlg.FormShow(Sender: TObject);
begin
// populate attributes
with SpeedButton2 do begin
  Parent := Self;
  Caption := '2';
  Height := 25;
  Width := 25;
  Left := 60;
  Name := 'SpeedButton2';
  Tag := 2;
  Top := 80;
  OnClick := SpeedButtonClick;
  end;
with SpeedButton3 do begin
  Parent := Self;
  Caption := '3';
  Height := 25;
  Width := 25;
  Left := 100;
  Name := 'SpeedButton3';
  Tag := 3;
  Top := 80;
  OnClick := SpeedButtonClick;
  end;
end;

procedure TOKBottomDlg.SpeedButtonClick(Sender: TObject);
begin
// shows which SpeedButton is pressed
MessageDlg('Speedy button:'+IntToStr(TControl(Sender).Tag),mtConfirmation,[mbYes,mbNo,mbCancel],0);
end;
end.

Comments:
I have dropped a TSpeedButton just for testing purposes
     SpeedButton1 in my case

Buttons are created with the main form as owner. This avoids the need to free them.

The same event is shared by all buttons.

Attributes are assigned in main form OnShow event
Notice Parent assignment: this is currently the main form (Self) but it could and must be any other showable frame. Without this Parent assignment, SpeedButtons will never be shown.

Everything is in place.
Compile and Run the program with this form
When clicking on one of the three SpeedButtons, the message dialog box will show the corresponding tag value.

With further development, buttons could be created within an ObjectList; any other TNotifyEvent of the same type could be assigned.

Don't forget that SpeedButton are not windows but only gadgets: they do not have a window handle.

Hope this helps.

Antonio.
0
kretzschmarCommented:
not sure for what you are after,

but a try

//MainForm
unit frame_test_main_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses frame_u;

{$R *.dfm}



procedure TForm1.FormClick(Sender: TObject);
begin
  if (sender is TComponent) then
    ShowMessage(TComponent(Sender).Name+' has Clicked!')
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TFrame2.Create(self,FormClick) do
  begin
    parent := self;
    show;
  end;
end;

end.


//a Frame Unit
unit frame_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons;

type
  TFrame2 = class(TFrame)
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
  private
    { Private declarations }
  public
    constructor Create(AOwner : TComponent; AClickEventHandler : TNotifyEvent); OverLoad;
    { Public declarations }
  end;

implementation

{$R *.dfm}

constructor TFrame2.Create(AOwner : TComponent; AClickEventHandler : TNotifyEvent);
begin
  inherited Create(AOwner);
  if assigned(AClickEventHandler) then
  begin
    SpeedButton1.OnClick := AClickEventHandler;
    SpeedButton2.OnClick := AClickEventHandler;
    SpeedButton3.OnClick := AClickEventHandler;
    SpeedButton4.OnClick := AClickEventHandler;
    SpeedButton5.OnClick := AClickEventHandler;
    SpeedButton6.OnClick := AClickEventHandler;
  end;
end;


end.


meikl ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CleanupPingCommented:
jasongth:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.