Problem with form placement

Asked

Viewed 518 times

2

I have an application that works as follows: There is a main form and I open "children" forms during execution, for registration, research, etc. The intention is to always open the child forms in the center of the main form, regardless of the size of the form. For this use the following procedure (in the onCreate of the child form):

formulario->Left=(formPrincipal->pnlPrincipal->Width/2);
formulario->Top=(formPrincipal->pnlPrincipal->Height/2)-(formulario->Height/2);

Until the right moment, the form is centered correctly, as shown in the following image: inserir a descrição da imagem aqui

When closing the child form, restoring the main form and reopening the child form, the following problem occurs: inserir a descrição da imagem aqui

The child form is generated at the position that was generated the first time (with the window maximized).

2 answers

1

This happens because the event OnCreate is executed only once, when the form is created. You must put this code in the event OnShow, which occurs each time the form is displayed.

void __fastcall Tformulario::FormShow(TObject *Sender)
{
    formulario->Left = (formPrincipal->pnlPrincipal->Width/2);
    formulario->Top = (formPrincipal->pnlPrincipal->Height/2)-(formulario->Height/2);
}
  • One of the reasons was that of onCreate itself, but the problem wasn’t just that. I was taking as a reference the size of a panel. To solve the problem I started using the size of the form as reference, I started using the code in onShow and changed the property Position do form filho ara poDefault.

  • @Jefersonleonardo I even thought to suggest this, but you had mentioned that the first time the form was positioned correctly, so I didn’t quote it. The position of form son shouldn’t be poMainFormCenter?

  • At first it had worked well with poDefault I’ll redo some tests here. Thank you.

  • So I did some research and testing. To get to what I wanted I didn’t even need this code done in onShow. The position of the child form did not update when it was closed and started again because, for this, it is necessary to destroy the form and recreate it. And the position property of the child form must be poMainFormCenter

  • @Jefersonleonardo Since that’s what you solved, post it as an answer and accept it. Maybe it helps other people with similar problems. :)

0

The question has a DELPHI tag, so I imagine I can give the answer within this context.

For a form to appear centralized in relation to another form, it is necessary that the Owner of the second form be the first form and that the second form has the property Position configured as poOwnerFormCenter.

To create a form, having as Owner another form, proceed as follows:

SegundoForm := TSegundoForm.Create(PrimeiroForm);
SegundoForm.ShowModal;

Using the above creation form, Secondform will be shown in front of Primeiroform and right at its center.

When finished using Secondform, remember to destroy it completely (Close and Free) or use Action := caFree at your event Onclose

Browser other questions tagged

You are not signed in. Login or sign up in order to post.