set Form StartPosition at runtime

  • Thread starter Thread starter sklett
  • Start date Start date
S

sklett

I thought this would be easy. I want to position a login form based on
whether or not it has a parent form, so I tried this:

<code>
if (ParentForm == null)
{
StartPosition = FormStartPosition.CenterScreen;
}
else
{
StartPosition = FormStartPosition.CenterParent;
}
</code>

I tried adding this in the following places:
- constructor
- Shown
- Load

It doesn't seem to have any effect. I'm thinking that it's not happening
*soon* enough, but I don't know how to catch it sooner than the constructor.
Any ideas?
 
There are a couple of reasons for why it may not be working but without
seeing your code, I'd be guessing. Anyway, you can accomplish the same goal
by setting the StartPosition in the designer to the default
(FormStartPosition.WindowsDefaultLocation) and then calling
Form.CenterToScreen() or Form.CenterToParent as necessary in Form.Load (or
within the constructor).

-sb
 
You should put that code in constructor, *after* the call of
InitializeComponent.
If you put it in Load or Shown, use CenterToScreen and CenterToParent
methods instead.
 
Back
Top