I create a new form and call from the parent form as follows:
loginForm = new SubLogin();
loginForm.Show();
I need to display the child form at the centre of the parent. So,in the child form load I do the foll:`
Point p = new Point(this.ParentForm.Width / 2 - this.Width / 2, this.ParentForm.Height / 2 - this.Height / 2);
this.Location = p;
But this is throwing error as parent form is null. I tried setting the Parent property as well, but didn't help. Any inputs on this?
Try:
Of course the child for will now be a blocking form (dialog) of the parent window, if that isn't desired then just replace
ShowDialog
withShow
..You will still need to specify the StartPosition though.
You need this:
Replace Me with this.parent, but you need to set parent before you show that form.
On the SubLogin Form I would expose a SetLocation method so that you can set it from your parent form:
Then, from your main form:
The setting of parent does not work for me unless I use
form.ShowDialog();
.When using
form.Show();
orform.Show(this);
nothing worked until I used,this.CenterToParent();
. I just put that in the Load method of the form. All is good.Start position to the center of parent was set and does work when using the blocking showdialog.
When launching a form inside an
MDIForm
form you will need to use.CenterScreen
instead of.CenterParent
.The parent probably isn't yet set when you are trying to access it.
Try this: