Board index » delphi » Using TAction in different form

Using TAction in different form


2003-11-24 09:45:42 PM
delphi88
Hi,
I have two forms:
Form2 with One TActionList with one TAction having a OnExecute event:
procedure TForm2.Action1Execute(Sender: TObject);
begin
showmessage ('The action is performed.')
end;
And a Form1 with one TPopupMenu, with one TMenuItem, which is linked
to the Form2.Action1 (of course Unit2 is in my uses clause).
Why is the action not executed when I click het menu item ?
Am I missing something?
Thanks,
Anja.
 
 

Re:Using TAction in different form

Hi,
I have two forms:
Form2 with One TActionList with one TAction having a OnExecute event:
procedure TForm2.Action1Execute(Sender: TObject);
begin
showmessage ('The action is performed.')
end;
And a Form1 with one TPopupMenu, with one TMenuItem, which is linked
to the Form2.Action1 (of course Unit2 is in my uses clause).
Why is the action not executed when I click het menu item ?
Am I missing something?
Thanks,
Anja.
 

Re:Using TAction in different form

Hi Anja,
it's difficult to say anything without more information.
Does your form2 exist when you call the action of form2?
Bye Ollo
"Anja" <XXXX@XXXXX.COM>schrieb im Newsbeitrag news:3fc31d58$XXXX@XXXXX.COM...
Quote

Hi,

I have two forms:

Form2 with One TActionList with one TAction having a OnExecute event:

procedure TForm2.Action1Execute(Sender: TObject);
begin
showmessage ('The action is performed.')
end;

And a Form1 with one TPopupMenu, with one TMenuItem, which is linked
to the Form2.Action1 (of course Unit2 is in my uses clause).

Why is the action not executed when I click het menu item ?
Am I missing something?

Thanks,
Anja.

 

Re:Using TAction in different form

In article <3fc31d58$XXXX@XXXXX.COM>, Anja writes:
Quote
Form2 with One TActionList with one TAction having a OnExecute event:

procedure TForm2.Action1Execute(Sender: TObject);
begin
showmessage ('The action is performed.')
end;

And a Form1 with one TPopupMenu, with one TMenuItem, which is linked
to the Form2.Action1 (of course Unit2 is in my uses clause).

Why is the action not executed when I click het menu item ?
Am I missing something?
IMO it is a very bad idea to link two forms this way. The action is
probably disabled or the link you set in the designer has been lost.
This will happen when the application loads TForm1 and TForm2 has not
been created yet. Set the link at run-time, but not in the OnCreate
event of form1, that is too early. Use OnActivate or OnShow.
Peter Below (TeamB)
Use the newsgroup archives :
www.mers.com/searchsite.html
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be
 

Re:Using TAction in different form

"Peter Below (TeamB)" <XXXX@XXXXX.COM>writes:
Quote
In article <3fc31d58$XXXX@XXXXX.COM>, Anja writes:

>Form2 with One TActionList with one TAction having a OnExecute event:
>
>procedure TForm2.Action1Execute(Sender: TObject);
>begin
>showmessage ('The action is performed.')
>end;
>
>And a Form1 with one TPopupMenu, with one TMenuItem, which is linked
>to the Form2.Action1 (of course Unit2 is in my uses clause).
>
>Why is the action not executed when I click het menu item ?
>Am I missing something?

IMO it is a very bad idea to link two forms this way. The action is
probably disabled or the link you set in the designer has been lost.
This will happen when the application loads TForm1 and TForm2 has not
been created yet. Set the link at run-time, but not in the OnCreate
event of form1, that is too early. Use OnActivate or OnShow.

--
Peter Below (TeamB)
Use the newsgroup archives :
www.mers.com/searchsite.html
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be


Hi Peter,
I've tested your idea to link the action at run-time (just in a Onclick event of a button) and then it works. Great!
But seems to me a strange behaviour, as I can 'link' two forms by using eg. a TTable from one form as DataSet property of a TDatasource in another form, without any problem.
My idea was: let all the actions which have an influence on TForm2 reside on that form, even if they are invoked by a TMenuItem on TForm1. Would you propose another way of working?
But in the meanwhile, I can continue with your solution.
Could you please take a look on my Primary Key problem, posted in the vcl.components.writing NG?
Anja.
 

Re:Using TAction in different form

In article <3fc45d24$XXXX@XXXXX.COM>, AnjaSmedts writes:
Quote
But seems to me a strange behaviour, as I can 'link' two forms by using
eg. a TTable from one form as DataSet property of a TDatasource in
another form, without any problem.
Oh, there are reports about similar problems with such setups quite regulary
on these groups. The typical setup using datamodules works because the DMs
are created before the main form is. Even then you can run into problems if
you close the DM in the IDE, then change something on a form that has links to
the DM and save the form.
Quote

My idea was: let all the actions which have an influence on TForm2 reside
on that form, even if they are invoked by a TMenuItem on TForm1. Would
you propose another way of working?
Yes, i never let code in unit A mess around with the innards of an object
in unit B. Basically i consider all the stuff the IDE puts in the default
section of a form class to be private to the form (even though it is
published in scope). So the menu item in your case would have a handler on
form1 and that handler would call a public method in form2 that would then
call the form2 actions Execute method (or better call a method that does the
actual work and is also called form the actions OnExecute event).
Doing things this way may look like more work but it isolates your forms
much better from each other, and that pays of big over time. You can now
change form2, rename the action, whatever, without breaking code in form1.
Peter Below (TeamB)
Use the newsgroup archives :
www.mers.com/searchsite.html
www.tamaracka.com/search.htm
groups.google.com
www.prolix.be