Reputation: 169
I have an application that works as follows: There is a main form which are open child forms for registration, search, etc. The intention is always open child forms in the center of the main form, regardless of the size of it. For this I am using the following code in the form son onCreate:
formChild->Left=(MainForm->pnlMain->Width/2);
formChild->Top=(MainForm->pnlMain->Height/2)-(formChild->Height/2);
So far everything right, the form is centered correctly, as shown in the following image:
When the child form is closed, the main form is restored and the son form is opened, the following problem occurs:
The child form is generated in the position that was generated the first time (with the maximized window).
Upvotes: 0
Views: 455
Reputation: 8396
The correct way to deal with this is to set the Child Form's Position
property to poMainFormCenter
.
This will make sure that when the child form is created and shown for the first time, it will be centered on the MainForm.
NOTE, the Child Form's position won't be updated if you simply hide it and then show it again later. If you need the Child Form to always be centered each time it is shown, you should destroy it when it is closed and then re-create it before showing it.
Upvotes: 5