I’m having trouble getting a child form to center itself wrt its parent. I’ve been trying all sorts of suggestions ranging from using or not using ShowDialog to all kinds of whacky “Calculate your form’s position” code.
This really shouldn’t be so complex, I think.
Anyway, here’s some code. The following code is where I instantiate the form and ask it to open. The this in this code is the MainForm.
FormName myForm = new FormName();
myForm.ShowDialog(this);
I have set the form’s StartPosition to CenterParent but if I move the MainForm around and reopen the form, it doesn’t follow along. In fact, it stays right where it is no matter what. It’s not even center-screen!
In the form’s constructor, I do not set anything wrt the form’s position.
I also do not set anything wrt the form’s position in the Load method of the form.
Please help!
Edit:
I’ve tried stepping through the constructor and load methods of the form to see if they bailout before finishing and they work just fine.
It turns out that adding code to the constructor of a form is a bad idea and causes all sorts of weird behavior. Instead, one should add code for the form on the Form Load event. This was the root of my problem.