Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-31
3
Medium Priority
?
183 Views
Last Modified: 2010-04-06
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?
0
Comment
Question by:jasongth
3 Comments
 

Expert Comment

by:antonio_fortuny
ID: 8243716
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 375 total points
ID: 8243754
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
 

Expert Comment

by:CleanupPing
ID: 9316519
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

575 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