Positioning winform to the center screen does not work

c# form location center screen
c# form start position center parent
c# set form location programmatically
windows form start position bottom right
which method starts the web forms application and displays the specified form on the screen?
vb.net set form location programmatically
windowsdefaultlocation
powershell form startposition values

I have an issue regarding the location of the window in my Winforms application.

I need to position the window in the center of the screen during start up. I tried the following but that didn't work. The form always opens at the top left corner.

I tried these in the load event:

this.CenterToScreen();
this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                          (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);

Update: i have given the form maximum and minimum size as 1090,726.

my designer file has this code:

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.GrayText;
        this.ClientSize = new System.Drawing.Size(1074, 688);
        this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
        this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
        this.MaximizeBox = false;
        this.MaximumSize = new System.Drawing.Size(1090, 726);
        this.MinimumSize = new System.Drawing.Size(1090, 726);
        this.Name = "Mail_Remainder";
        this.RightToLeftLayout = true;
        this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Remainder";
        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        this.Load += new System.EventHandler(this.Mail_Remainder_Load);
        this.Resize += new System.EventHandler(this.Mail_Remainder_Resize);
        this.ResumeLayout(false);
        this.PerformLayout();

Any other way to do this?

Please help.

Give a look at the following:

 Form1.StartPosition = FormStartPosition.CenterScreen;
 Form1.Show();

Edit:

The line

 this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

is the one causing the decentering.

If you comment it:

    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // Form1
        // 
                    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.GrayText;
        this.ClientSize = new System.Drawing.Size(1074, 688);
        this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
        this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
        this.MaximizeBox = false;
        this.MaximumSize = new System.Drawing.Size(1090, 726);
        this.MinimumSize = new System.Drawing.Size(1090, 726);
        this.Name = "Mail_Remainder";
        this.RightToLeftLayout = true;
        this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Remainder";
//            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        this.Load += new System.EventHandler(this.Mail_Remainder_Load);
        this.Resize += new System.EventHandler(this.Mail_Remainder_Resize);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

the window appears on the center of the screen.

Center Screen not working - MSDN, /windows/en-US/3bcfd2e8-53bb-4e1f-ba7d-28a4d5b83d80/center-screen-not- working?forum=winforms Question 2 At the same time the form will display in the center of the screen. Can anyone help me to work around for this / why this is happened? Manual; // set position and size to the Form. this. My form does not center. I set it in the Properties and it shows up upper left. I then set it to StartPosition Manual and tried to set X and Y to move it over and down. This does not work either no movement. I then added InitForm code to calc screen size and move to different location. None of these work and its driving me nuts. Help!

Just set the StartPosition property of your form to be CenterScreen .....

Form.StartPosition Property (System.Windows.Forms), Gets or sets the starting position of the form at run time. You can also position the form to display in the center of the screen or in the center of its parent form for � I have an issue regarding the location of the window in my Winforms application. I need to position the window in the center of the screen during start up. I tried the following but that didn't work. The form always opens at the top left corner. I tried these in the load event:

I had a similar problem; after CenterToScreen () the form was off-center. Then I noticed I was doing this BEFORE InitializeComponent (). I moved it after InitializeComponent and it worked fine.

Center a form on the screen at run time in C#, Note that you cannot do this in the form's Load event handler because the form's position is already set when that event handler executes. You� A Point that represents the converted Point, p, in screen coordinates. Examples. The following code example demonstrates how to use the BackColor, RectangleToScreen, PointToScreen, MouseButtons, ControlPaint.DrawReversibleFrame, and Rectangle.IntersectsWith members. To run the example, paste the following code in a form called Form1 containing

GDI+ Programming in C# and VB .NET, Set the form's start-up position to be center screen. 3. Add a horizontal scroll bar. Name it BlendWidth. Placement is not critical. 4. Add a horizontal scroll bar. Here we adjust the position of the window before it is shown. Look at the Screen.PrimaryScreen WorkingArea.Height and Width properties above. These are the most important trick to this project. Here: We see the approach we will use to position windows like a palette relative to another window. This will position to the bottom.

C# Windows Forms, Enables you to set the start position of a form at run-time CenterScreen, Form is centered on the current display with dimensions specified in the forms size. A maximized window does not cover a taskbar that has the "Always on top" property. However, a full-screen window covers the taskbar--for example, in Microsoft PowerPoint slide shows and games. To save, and later restore, the position of a window when an application exits, use the GetWindowPlacement and SetWindowPlacement functions.

DesktopAlert Control - Telerik UI for WinForms Components, Telerik RadDesktopAlert control for WinForms ensures that you track all You can position the alert's popup on the screen to best fit your scenario or preferences. bottom-center, bottom-right and manual, which enables you to display the alert at You do not need to manually adjust the position of the alerts as they will be� I have configured a few of the windows I am working on to have the form value "Start Position" to be "CenterParent" but some of them don't. Does Center Parent only work when the dialog is modal? I'm not writing an MDI application so I other posts I've read just don't seem very relevent. Do I have to explicitly set the parent??? What if I don't

Comments
  • You should do this in OnShow, as the size may not have been applied in OnLoad.
  • @karthi you have only to comment the line this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
  • You need to remove the code that manually repositions the form.
  • No, you said that in OnLoad you added the code you quoted from your question. Of course you need to remove that.
  • @ThorstenDittmar i added that only after this didn't work.i have did this earlier but not geting it to work now
  • Then there's some other place where you reposition the form manually. Setting StartPosition to CenterScreen always worked for me in the past.
  • @marc_s i have another form which works fine with the same thing :-(