Link to home
Start Free TrialLog in
Avatar of pjroy
pjroy

asked on

Getting parent

A form1 call form2 like this: form2.show;

How can I know the parent of form2 so I can do something like this in form2: left := parent.left; top := parent.top
ASKER CERTIFIED SOLUTION
Avatar of rickpet
rickpet

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
Avatar of pjroy
pjroy

ASKER

Perfect, it work when the parent is a form. But now I want the parent to be a DBEdit. But it the form2 doesn't position correctly. Why? In a DBEdit control, I want when a user press enter that form2 show up. I want form2 position to be left=left of DBEdit and top=bottom of DBEdit.

The result of this code is correct whene the parent is a form, but doesn't work when the parent is DBEdit:
  Left := (frmListClient.Owner as TWinControl).Left;
  Top := (frmListClient.Owner as TWinControl).Top;

What is the difference?

remember there is a difference between parent and owner.
Parent is used for visual relationships.  I.E. CheckBox1's Parent can be GroupBox1.  Note when this happens CheckBox1 can only be shown in the window of GroupBox1.

Now CheckBox's owner will most likely be Form1.  Owner is in charge of freeing.

So for a recap.
Parent is for window relationships.
Owner is for memory.

To get your DBEdit to align correctly...try this...

Form2.Left := (Sender as TWinControl).Left + (Sender as TWinControl).Parent.Left;
Form2.Top := (Sender as TWinControl).Top + (Sender as TWinControl).Parent.Top;
  Form2.show;

Rick