views:

804

answers:

3

Hi Everyone, I am trying to center a form in VB.net. Instead of centering the form, it ends up about halfway between center and 0,0(upper left).

I am using the code

Me.StartPosition = FormStartPosition.CenterScreen

Which is called from the IntializeDisplay Method, which in turn is called from the Form Load method.

I assume I'm setting some propertity along the way that messes up the center calculation, but I'm not sure what it could be.

If anyone has any ideas they would be much appreciated.

Thanks.

A: 

Do you have any form resizing/positioning logic implemented? If so, comment it out and try again.

Try setting the Form.StartPosition in the designer (which will set it in InitializeComponent()) instead of in the Load event.

Try resetting the Form.Location and Form.Size value. If your form is localized, remove the Form.Location AND Form.Size entry in the resource file.

Vincent Van Den Berghe
Thanks I'll give those a try and get back to you.
Charlie White
+1  A: 

I think you're setting the value of StartPosition too late in the flow - by the time Form.Load is called, Loading has already completed and the form has an assigned position.

Set a breakpoint on the line of code quoted in your question, and look at the forms position - it will already be in the location it appears.

To have the effect you want, the value of StartPosition needs to be set before the form starts its' built in location processing. I'd suggest putting the code in the form constructor, after the call to InitializeComponent().

Bevan
A: 

add this in the event and it will work:

Public Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    Me.StartPosition = FormStartPosition.CenterScreen

End Sub