Solved

Getting parent

Posted on 1998-06-05
3
239 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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