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
Solved

Attach a function to an event handler

Posted on 1997-11-20
6
161 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

791 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