.Net FormStartPosition.CenterScreen does not center
I am trying to center a form in VB.net. Rather than centering the shape, it ends up about halfway between center and 0.0 (top left).
I am using code
Me.StartPosition = FormStartPosition.CenterScreen
which is called from the IntializeDisplay method, which in turn is called from the Load Load method.
I am guessing that I am setting some properties on the path that messed up the center calculation, but I am not sure what that might be.
If anyone has any ideas, they will be highly appreciated.
Thank.
add this to 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
I think you are setting the StartPosition value too late in the thread - by the time Form.Load appears, the Loading is already complete and the form has an assigned position.
Set a breakpoint on the line of code mentioned in your question and look at the position of the form - it will already be where it is displayed.
To get the desired effect, the StartPosition value must be set before the form can begin its "inline position processing". I would suggest putting the code in the form constructor after calling InitializeComponent ().
If you are resizing the form or using auto size, you need to use
Me.CenterToScreen()
at the end of the Load event.
Is there some form of resizing / positioning logic? If yes, please comment on this and try again.
Try setting Form.StartPosition
in the constructor (which will set it to InitializeComponent()
) rather than the Load event.
Try to reset the values Form.Location
and Form.Size
. If your form is localized, remove the Form.Location
AND entry Form.Size
in the resource file.
Instead of relying on form initialization to handle its original location, try calling a method to center the form. This allows you to center the form even after resizing after showing.
Example:
Me.CenterToScreen()
or
Me.CenterToParent()
Depending on where you are trying to center the shape.