4

I have a form called Form1. Its set to startup-Position = Center but when executed it opens up somewhere else (At a random position evrytime).

I am working under Windows XP SP3 , using IDE Visual Studio - 2010. Please provide a workaround to this problem.

I have uploaded a sample project showing the above mentioned problem .

Download link:

http://www.6ybh-upload.com/vt5i4z1wz9pl/Light.zip

SpongeBob SquarePants
  • 1,035
  • 12
  • 26
  • 46

6 Answers6

9

You have to set:

Form1.StartPosition = FormStartPosition.Manual

Edit:

Here is a working sample:

Dim X As Integer = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
Dim Y As Integer = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2
Me.StartPosition = FormStartPosition.Manual
Me.Location = New System.Drawing.Point(X, Y)

Edit 2:

Here is the improved code based on comments by Hans Passant, (much better):

Dim mainScreen As Screen = Screen.FromPoint(Me.Location)
Dim X As Integer = (mainScreen.WorkingArea.Width - Me.Width) / 2 + mainScreen.WorkingArea.Left
Dim Y As Integer = (mainScreen.WorkingArea.Height - Me.Height) / 2 + mainScreen.WorkingArea.Top

Me.StartPosition = FormStartPosition.Manual
Me.Location = New System.Drawing.Point(X, Y)
Davido
  • 2,913
  • 24
  • 38
  • How will that help me center my form ? – SpongeBob SquarePants Mar 03 '11 at 15:50
  • You have to set that value, then set your form1.startPosition value to whatever start position you want. If startposition isn't set to manual, then it will pick where to start. If it's manual, you decide where it starts. So now you can manually calculate the start location to place it at the center of the screen. – Davido Mar 03 '11 at 16:12
  • But how can I calculate the x,y co-ordinates (location) if I want my form to be center ? Can you update your answer with an example ? – SpongeBob SquarePants Mar 03 '11 at 16:25
  • 2
    It is assumes the primary screen is at 0, 0. It doesn't have to be. Add Bounds.Left and Bounds.Top to the X and Y values. And you should use WorkingArea, not Bounds. And the form might not necessarily be opened on the primary screen, use Screen.FromPoint. And it should be done in the Load event to deal with AutoScaleMode. Details, details. – Hans Passant Mar 03 '11 at 18:05
  • @Hans Passant: It would be better if you could put the above in an answer so that newbies like me could understand it clearly and avoid any misundrstanding. (Just a suggestion :D ) – SpongeBob SquarePants Mar 03 '11 at 21:29
  • I was hoping that Davido would use it to improve his answer. – Hans Passant Mar 03 '11 at 21:35
  • @Hans Passant: If Davido fails to improve his answer in 2-3 days then please post your answer – SpongeBob SquarePants Mar 03 '11 at 22:10
2

Try to use this after resize the screen

Me.Size = New System.Drawing.Size(800, 436)
Me.CenterToScreen()
John Koerner
  • 37,428
  • 8
  • 84
  • 134
Luiey87
  • 21
  • 1
  • Note the edit I made to your answer. I put 4 spaces in front of the code lines to indicate they were code and the formatting was applied to them. – John Koerner Jan 17 '13 at 03:19
1

Here is the solution:

    Dim screen__1 As Screen = Screen.FromControl(frm)
    Dim workingArea As Rectangle = screen__1.WorkingArea
    frm.Location = New Point() With { _
     .X = Math.Max(workingArea.X, workingArea.X + (workingArea.Width - frm.Width) / 2), _
     .Y = Math.Max(workingArea.Y, workingArea.Y + (workingArea.Height - frm.Height) / 2) _
    }
DareDevil
  • 5,249
  • 6
  • 50
  • 88
1

The Second One:

    'frm = is the form object
    Dim X As Integer = (Screen.PrimaryScreen.Bounds.Width - frm.Width) / 2
    Dim Y As Integer = (Screen.PrimaryScreen.Bounds.Height - frm.Height) / 2
    frm.StartPosition = FormStartPosition.Manual
    frm.Location = New System.Drawing.Point(X, Y)
DareDevil
  • 5,249
  • 6
  • 50
  • 88
1

In your question it isn't quite clear what you have actually tried since there is no such option as "Center" for the StartPosition property of a Form.

However, setting StartPosition to CenterScreen or Me.StartPosition = FormStartPosition.CenterScreen if you are doing it programmatically, should get you exactly what you need.

Reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.formstartposition.aspx

Yetti
  • 1,710
  • 1
  • 14
  • 28
0

For VB.net 2010 put code to form load event

Call CenterToScreen()

this is built in method provided by VS