I have a MainForm with a subform SubLevel1 within a panel. In SubLevel1 I have another subform SubLevel2 which is opened in a panel within SubLevel1. Now, from SubLevel2 I am opening a ModalForm with ShowDialog()
and I want to center it above SubLevel2 resp. MainForm (in which SubLevel2 is included through SubLevel1).
CenterParent
just does not work and am not able to get the correct (relative) location to position the ModalForm correctly:
I am not able to set SubLevel2 as
Parent
of the ModalForm: "a top level control can not be added to a control" error pops up. Therefore,Parent
of the ModalForm is alwaysnull
.When I set SubLevel2 as
Owner
of ModalForm, it always has the location0,0
which cannot be used to position the ModalForm.When I use
ownerLocation = modalForm.Owner.PointToClient(Point.Empty)
the position is not correct.When I use
ownerLocation = modalForm.Owner.PointToScreen(Point.Empty)
the position is not correct.
When creating the SubForms I do it as follows:
_FormSub = new FormSub() {
TopLevel = false,
TopMost = false
};
panelSub.Controls.Add(_FormSub);
_FormSub.Show();
When creating the ModalForm within the code of SubForm2 I do it as follows:
formModal = new formModal() {
Owner = this
};
formModal.ShowDialog();
What do I need to change?