Problem with form placement

1

I have an application that works as follows: There is a main form and I open "child" forms during execution, for registration, search, etc. The intent is always to open the child forms in the center of the main form, regardless of the size of the form. To do this I 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);

So far right, the form is centered correctly, as shown in the following image:

Whenyouclosethechildform,restoretheparentform,andthenreopenthechildform,thefollowingproblemoccurs:

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

    
asked by anonymous 19.08.2016 / 19:45

2 answers

0

This happens because the OnCreate event runs only once, when the form is created. You should put this code in the OnShow event, 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);
}
    
20.08.2016 / 00:57
0

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

For a form to appear centralized relative to another form, the Owner of the second form must be the first form and the second form must have the Position property set to poOwnerFormCenter .

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

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

Using the above form of creation, SecondForm will appear in front of FirstForm and exactly in its center.

When you are finished using SegundoForm , remember to destroy it completely (Close and Free) or use Action: = caFree in your OnClose

    
20.10.2016 / 20:31