Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

A procedure in a component calls a main-menu-item on the main form,part2

Dear Experts,

Hi, i have asked this question before, and without testing the code that I have
received I have awarded the points. And after givin the point I found out that
the code didn't work. My fault.

This was my previous question:
https://www.experts-exchange.com/questions/22494756/a-procedure-in-a-component-calls-a-main-menu-item-on-the-main-form.html

I have put this procedure in my component:

1. procedure TMyDrawingPanel.CursorFlashTimer_OnTImer(sender: TObject);
2. begin
3. CursorFlashTimer.Enabled := False;
4. try
5. CursorVisible := not CursorVisible;
6. finally
7. // if (not CursorVisible) or (miFlashCursor.Checked) then CursorFlashTimer.Enabled := True;
8. end;
9. end;

I get an error at line 7.

Because line 7 referse to a main-menu-item called miFlashCursor on the main form.

Is there a way to do this, that a procedure in a component calls a main-menu-item
that is on the main form, or any other form?

Who knows the answer and is willing to help me?

Greetings,

Peter Kiers
Avatar of TName
TName

Hi, I'm afraid I'll have to give you a very similar answer to the one Eddie did in the mentioned PAQ.
Correcty applied, his first answer should work  IF the parent of your component's instance is the main form.
If not, please tell us how the component is placed on the form...

BTW, things would be much easier if you'd always mention what exactly doesn't work, e.g. quote the exact error message... It is very frustrating to give an answer (that you're quite confident should work) and then only hear: It doesn't work.

Let's take this example: If for instance you do what Eddie advised you to do, and you try
(TMainForm(Self.Parent).miFlashCursor.Checked)  // let's presume your main form is of the type TMainForm...
and you get an error message saying "Undeclared identifier TMainForm", then we know that the main form's unit isn't included in the component's uses clause.

So, did you try something like this (you have to typecast Self.Parent to whatever your main forms CLASS is!):

 //if  for instance your main form is called Form1 then
 if  {...}  (TForm1(Self.Parent).miFlashCursor.Checked) then CursorFlashTimer.Enabled := True;


 //if  for instance your main form's is called MainForm then
 if  {...}  (TMainForm(Self.Parent).miFlashCursor.Checked) then CursorFlashTimer.Enabled := True;

And make sure your main form is included in your component's uses clause. To avoid a circular reference you have to include it in the implementation section:

// In your component's unit !!
implementation
uses Unit1;    // or whatever your main form's unit name is!

If on the other hand the main form is NOT the component's parent (e.g. if the component is placed on a panel or scrollbox or the like), you can try to hardcode the form's name, just to see if it works:

   Form1.miFlashCursor.Checked   // or MainForm.miFlashCursor.Checked


If it still doesn't work, please say so and I will post a small working example application...
 
BTW, if for instance the component is placed on a panel which is placed on the form, then you could also try .Parent.Parent:
("climbing up" the parent hierarchy...)

(TForm1(Self.Parent.Parent).miFlashCursor.Checked)
Avatar of Peter Kiers

ASKER

I had allready done it, but I get an EAccessViolation at this line:

    if (not CursorVisible) or (TMainForm(Self.Parent).miFlashCursor.Checked) then

I can't find out what I am doing wrong?

Peter
ASKER CERTIFIED SOLUTION
Avatar of TName
TName

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This was the problem:

(TForm1(Self.Parent.Parent).miFlashCursor.Checked)

Now it works.

Thank you very much for helping me.

Greetings,

Peter Kiers
You're welcome, thanks!