Solved

How to share an event handler between forms?

Posted on 2008-10-15
3
340 Views
Last Modified: 2012-05-05
I have event handler code in one form of an application that I would like to use for a component event in another form of the application.  

Each of the forms lists each other in the uses statement. but I can't see handlers from the other form in an event dropdown in the object inspector and if I type in the name of the procedure (<other_form_name>.<proc_name>) I receive an IDE error that the name is not a valid identifier.

Thanks.
0
Comment
Question by:ttheimer
3 Comments
 
LVL 14

Expert Comment

by:SteveBay
ID: 22726080
That's right. You need to make the event handler assignment at runtime.
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 250 total points
ID: 22726090
let's say you want to use form 1's event on your form 2.
make sure
uses unit1
is in the "TOP" uses clause

and in unti1.pas, you have
uses unit2 in the bottom uses clause (after the implementation line)

It should work then

You can also assign it at runtime if you want

e.g. click on form 2, and then in the object inspector (F11) double click in it's "OnCreate" event
then put this code

procedure TForm2.FormCreate(sender: TObject);
begin
  Button1.OnClick := form1.ButtonAClick;
end;
0
 

Author Comment

by:ttheimer
ID: 22727853
TheRealLoki

I was able to define the event handlers upon execution using the second half of your answer.  

I tried setting up as described in the first half but never got it to work.  It made sense and I went back to check my setup but then ran out of time for further testing.  I will probably come back to this when more free time is available so I would be interested if this is a tested solution or a theory.

Thanks.
Tom
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now