none
Move a form to center position RRS feed

  • Question

  • Are there the best way to move a form to center position in runtime?
    Must calculate with the form size?

    Thanks,
    -Andrew
    Thanks, -Andrew Huang andrew.huang@ymail.com
    Tuesday, September 9, 2008 9:10 PM

Answers

  •  
    using System.Windows.Forms;  
    using System;  
    using System.Drawing;  
    namespace WindowsFormsApplication5  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
                this.StartPosition = FormStartPosition.CenterScreen;  
            }  
     
            private void Center(Form form)  
            {  
                form.Location = new Point((Screen.PrimaryScreen.Bounds.Size.Width / 2) - (form.Size.Width / 2), (Screen.PrimaryScreen.Bounds.Size.Height / 2) - (form.Size.Height / 2));  
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                Center(this);  
            }  
        }  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Andrew Huang Wednesday, September 10, 2008 6:42 PM
    Tuesday, September 9, 2008 9:51 PM
    Moderator
  • I can't think off the top of my head of a function like CenterOnScreen() or something like that. On the other hand, calculating with the form size is relatively simple. You want the top-left corner of the form (the origin) to be at the position in the screen such that the center of the screen coincides with the center of the form.

    Assuming you have the screen bounds and assume it's in a Rectangle variable stored as bounds. Then this code should work.

    int x = bounds.Width / 2 - this.Width / 2;  
    int y = bounds.Height / 2 - this.Height / 2;  
    this.Location = new Point(x, y); 

    "A cat always lands feet first. Toast always lands butter side down. A cat with toast strapped on its back will hover above the air in a state of quantum indecision." ~Unknown
    • Marked as answer by Andrew Huang Wednesday, September 10, 2008 6:42 PM
    Tuesday, September 9, 2008 9:24 PM

All replies

  • I can't think off the top of my head of a function like CenterOnScreen() or something like that. On the other hand, calculating with the form size is relatively simple. You want the top-left corner of the form (the origin) to be at the position in the screen such that the center of the screen coincides with the center of the form.

    Assuming you have the screen bounds and assume it's in a Rectangle variable stored as bounds. Then this code should work.

    int x = bounds.Width / 2 - this.Width / 2;  
    int y = bounds.Height / 2 - this.Height / 2;  
    this.Location = new Point(x, y); 

    "A cat always lands feet first. Toast always lands butter side down. A cat with toast strapped on its back will hover above the air in a state of quantum indecision." ~Unknown
    • Marked as answer by Andrew Huang Wednesday, September 10, 2008 6:42 PM
    Tuesday, September 9, 2008 9:24 PM
  • Just set the Form's StartPosition property to CenterScreen.
    David Morton - http://blog.davemorton.net/
    Tuesday, September 9, 2008 9:29 PM
    Moderator
  • Thanks, David.

    I did, but it doesn't work even though InitializeComponents(). I think it works only for starting an application.
    Thanks, -Andrew Huang andrew.huang@ymail.com
    Tuesday, September 9, 2008 9:45 PM
  •  
    using System.Windows.Forms;  
    using System;  
    using System.Drawing;  
    namespace WindowsFormsApplication5  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
                this.StartPosition = FormStartPosition.CenterScreen;  
            }  
     
            private void Center(Form form)  
            {  
                form.Location = new Point((Screen.PrimaryScreen.Bounds.Size.Width / 2) - (form.Size.Width / 2), (Screen.PrimaryScreen.Bounds.Size.Height / 2) - (form.Size.Height / 2));  
            }  
     
            private void button1_Click(object sender, EventArgs e)  
            {  
                Center(this);  
            }  
        }  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Andrew Huang Wednesday, September 10, 2008 6:42 PM
    Tuesday, September 9, 2008 9:51 PM
    Moderator