Unable to place the form in the center

I have a form Form1. Its set in startup-Position = Center, but when it is executed, it opens somewhere else (in a random evrytime position).

I am running Windows XP Service Pack 3 (SP3) using the Visual Studio 2010 IDE. Please provide a workaround for this problem.

I downloaded a sample project that shows the above problem.

Download link:

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

+4
source share
6 answers

You must install:

Form1.StartPosition = FormStartPosition.Manual 

Edit:

Here is a working example:

 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 Hans Passant comments (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) 
+9
source

Try using this after resizing the screen.

 Me.Size = New System.Drawing.Size(800, 436) Me.CenterToScreen() 
+2
source

In your question, itโ€™s not entirely clear what you actually tried, since there is no such parameter as โ€œCenterโ€ for the StartPosition property of the form.

However, setting StartPosition to CenterScreen or Me.StartPosition = FormStartPosition.CenterScreen if you do this programmatically, you should get exactly what you need.

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

+1
source

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) _ } 
+1
source

Second:

  '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) 
+1
source

For VB.net 2010, put the code to generate the download event

Call CenterToScreen ()

this is a built-in method provided by VS

0
source

Source: https://habr.com/ru/post/1342142/


All Articles