Solved

Getting parent

Posted on 1998-06-05
3
212 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:pjroy
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
rickpet earned 10 total points
ID: 1350743
PJRoy

procedure TForm2.FormCreate(Sender: TObject);
begin
   form2.parent := form1;
end;

procedure TForm2.FormShow(Sender: TObject);
begin
  Left := (Form2.Parent).Left;
  Top := (Form2.Parent).Top;
end;


I don't think this is what you want...Note that Form2 will be inside Form1.  

Form2 should not be autocreated...Project|Options remove Form2 from autocreate.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm2.Create(self);
  Form2.show;
  ShowMessage(Form2.Owner.Name);
end;

procedure TForm2.FormShow(Sender: TObject);
begin
  Form1.hide;
  Left := (Form2.Owner as TWinControl).Left;
  Top := (Form2.Owner as TWinControl).Top;
end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  (Form2.Owner as TWinControl).Show;
  Action := caFree;
  Form2 := nil;
end;

0
 

Author Comment

by:pjroy
ID: 1350744
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?

0
 
LVL 3

Expert Comment

by:rickpet
ID: 1350745
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
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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

759 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

19 Experts available now in Live!

Get 1:1 Help Now