Formal Delphi Form Position

I have a form opened by another form.

I set the position as poOwnerFormCenter so that a new form opens where the original was

However, when I move this new form and then return to the original, it is shown where it was when I first opened the new form, and not where I closed it.

How can i fix this?

Thank!

+3
source share
3 answers

I am a little confused by your question, so I will clarify what I am trying to solve here! ...

I think you are trying to do it

When Form2 opens, it is centrally located so that Form1 and Form1 are hidden.

When Form2 closes, Form 1 is displayed (exactly where it was hidden).

, , Form1 , Form2 .

, , ...

procedure TForm1.ButtonClick(Sender: TObject);
begin
  Form2.ShowModal;
end;

, Form2 form1, Form2 poOwnerFormCenter

, , , , Form1, Form2 ,

procedure TForm1.ButtonClick(Sender: TObject);
begin
  Form2.ShowModal;
  Left := Form2.Left;
  Top := Form2.Top;
end;
+2

( ), , .

with TForm2.Create(nil) do
  try
    ShowModal;
  finally
    Free;
  end;

TForm2 , ; , TForm2 - TForm2. (, , )!

- TForm2 " " . , ( , ), , ...

IDE Screenshot

, Unit1 Unit2, Form2 Unit2 Form1, Unit1. Unit1 Alt + F11, .

Form2,

Form2.ShowModal;

Position . , , , , .

+1

The problem is that you are reusing the same instance of a modal form. Position preference only works when the form is first displayed. You have options here:

Option 1

You can destroy the modal form every time it closes. One way to do this is this line in the OnCloseform event :

Action = caFree;

Of course, this means that you must also update the modal form from the caller every time.

Option 2

You need to manually set the position of the modal form in the event OnShow.

Use the option that suits you best.

+1
source

All Articles