StartPosition is set to CenterPosition but my form is not centered
Asked Answered
B

1

6

I'm using Visual Studio 2012. My form, when it opens doesn't center to the screen. I have the form's StartPosition set to CenterScreen, but it always starts in the top left corner of my left monitor (I have 2 monitors).

Any ideas? Thanks

Bagby answered 12/1, 2013 at 12:20 Comment(4)
Placing it in the constructor works for me. Would you show us some code?Davinadavine
Which code do you need to see. I was under the impression I could set this using the form's properties?Bagby
Both placing StartPosition = FormStartPosition.CenterScreen; and setting it from the designer work for me. I've asked you for code to see if there's something that overwrites or interferes with your setting.Davinadavine
@user1936588 - ans updated!Schiro
S
7

try this way!

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            //Method 1. center at initilization
            this.StartPosition = FormStartPosition.CenterScreen;

            //Method 2. The manual way
            this.StartPosition = FormStartPosition.Manual;
            this.Top = (Screen.PrimaryScreen.Bounds.Height - this.Height)/2;
            this.Left = (Screen.PrimaryScreen.Bounds.Width - this.Width)/2;

        }
    }
}

Two virtual members are called in constructor of the application.

namely

this.Text; 
this.MaximumSize;

do not call virtual member in constructor it may lead to abnormal behaviour

fixed code

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();



            this.Location = new System.Drawing.Point(100, 100);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            // to see if form is being centered, disable maximization
            //this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Convertor";
            this.MaximumSize = new System.Drawing.Size(620, 420); 
        }
    }
}
Schiro answered 12/1, 2013 at 12:32 Comment(2)
Neither method 1 or 2 worked. This is the initialize code for form1 :- this.Location = new System.Drawing.Point(100, 100); this.MaximumSize = new System.Drawing.Size(620, 420); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Convertor"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout();Bagby
This worked for me, but not in the constructor. For some reason, form inheritance breaks CenterScreen, even after removing the inheritance that broke it. So the solution, for me at least, was to put the code from Method #2 in my base form's Load event handler.Bonsai

© 2022 - 2024 — McMap. All rights reserved.