Solved

Attach a function to an event handler

Posted on 1997-11-20
6
163 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
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!

 

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 50 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

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