Reputation: 2399
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.
Upvotes: 4
Views: 15264
Reputation: 525
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.
Upvotes: 1
Reputation: 3416
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.
Upvotes: 8
Reputation: 755421
Just set the StartPosition
property of your form to be CenterScreen
.....
Upvotes: 4