?
Solved

Attach a function to an event handler

Posted on 1997-11-20
6
Medium Priority
?
165 Views
Last Modified: 2010-04-04
How can I attach a function which is declared in another unit to an event handler, say OnDoubleClick. It seems what you attach in code must be declared in the class (published section).

Thanks,

Tom.
0
Comment
Question by:tomcorcoran
[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
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1351138
I think this is what you want,

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure WriteHallo;
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
 Form2.ShowModal;
end;

procedure TForm1.WriteHallo;
begin
 label1.Caption := 'Hello, Tom is this what you want?';
end;

end.

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1; { <-------- THIS NEEDS TO BE HERE }

{$R *.DFM}

procedure TForm2.Button1Click(Sender: TObject);
begin
 {Add Unit1 to uses list}
 Form1.WriteHallo;
end;

end.

0
 

Author Comment

by:tomcorcoran
ID: 1351139
?? I don't follow what you are trying.

What i want to do in thre form create is:

TPanel.OnDblClick := MyFunction;

where my function is a function declared in another unit.

Thanks,

Tom.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1351140
Ah, now I get it.
But my solution works too. If you push the button in Form2 then in Form1 the text in caption1 will be changed (thanks to procedure WriteHallo) -> look at form1 after pushing button on form2.

If you put in your TPanel.OnDblClick the following code :
FormName.FunctionName then this will be activated when you double click on tpanel. This is the easiest way to do it.
0
Technology Partners: 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!

 

Author Comment

by:tomcorcoran
ID: 1351141
Ok, the later makes sense, I'll do it that way, I should have thought of that. Put an answer in so I can grade you. Tom.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 200 total points
ID: 1351142
Ok, here is the example, same as the first one but now procedure will be called after you doubleclick the panel on form2.

- FORM1 ------------------------------------------------------

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure WriteHello;
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
 Form2.ShowModal;
end;

procedure TForm1.WriteHello;
begin
 label1.Color   := clRed;
 label1.Caption := 'Hello, Tom is this what you want?';
end;

end.

- FORM2 -------------------------------------------------------

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Panel1: TPanel;
    procedure Panel1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.DFM}

procedure TForm2.Panel1DblClick(Sender: TObject);
begin
 {Add Unit1 to uses list}
 Form1.WriteHello;
end;

end.

Have fun,
c.u. ZifNab;
0
 

Author Comment

by:tomcorcoran
ID: 1351143
calling the function in a handler was the way to go rather than trying to attach to the handler.
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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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