none
Center child window in parent RRS feed

  • Question

  • I have two forms form1 (parent) and form2 (child) set up using the code below. Run the code in VS and the forms load but the child form does not load in the center of the parent form or owner. How do I force the child form to load center of the parent form?

    ---
    // http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/174dd19b-34ca-4826-806e-0bc5115df651/
    //
    public partial class Form1 : Form
    {
        Form2 Frm2 = new Form2();
    
        public Form1()
        {
            InitializeComponent();
            Frm2.TextAvailable += new Form2.TextAvailableEventHandler(Frm2_TextAvailable);
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Frm2.Show();
            Frm2.textBox1.Text = "From Form1";
        }
    
        private void Frm2_TextAvailable(string T)
        {
            textBox1.Text = T;
        }
    }
    
    public partial class Form2 : Form
    {
        public delegate void TextAvailableEventHandler(string T);
        public event TextAvailableEventHandler TextAvailable;
    
        public Form2()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            TextAvailable("From Form2");
        }
    }
    
    Friday, November 6, 2009 4:10 AM

Answers

  • Hi,

    You can use this code. You don't have to use mdiparent.

                Form1 f1 = new Form1();
                f1.Show();
                int x=this.DesktopBounds.Left + (this.Width-f1.Width)/2;
                int y=this.DesktopBounds.Top + (this.Height-f1.Height)/2;
                f1.SetDesktopLocation(x, y);
    • Marked as answer by ReggieH Friday, November 6, 2009 3:59 PM
    Friday, November 6, 2009 7:56 AM

All replies

  • Hi,

    You can do it with StartPosition property of the form.

    Just before the line that you show form2 (Frm2.Show())

    Add one of this code.(depends on which position you want your form to be).

                f.StartPosition = FormStartPosition.CenterParent;
                //or
                f.StartPosition = FormStartPosition.CenterScreen;
    Friday, November 6, 2009 4:22 AM
  • Hi,
    public partial class Form1 : Form
    {
    
        Form2 Frm2 = new Form2();
        public Form1()
        {
    
            InitializeComponent();
    
            Frm2.TextAvailable += new Form2.TextAvailableEventHandler(Frm2_TextAvailable);
    
        }
    
    
    
        private void button1_Click(object sender, EventArgs e)
        {
    
            Frm2.Owner= this;
    
            Frm2.StartPosition= FormStartPosition.CenterParent;
    
            Frm2.Show();
    
            Frm2.textBox1.Text = "From Form1";
    
        }
    
    
    
        private void Frm2_TextAvailable(string T)
        {
    
            textBox1.Text = T;
    
        }
    
    }
    
    
    
    public partial class Form2 : Form
    {
        public delegate void TextAvailableEventHandler(string T);
    
        public  event TextAvailableEventHandler TextAvailable;
    
    
    
        public Form2()
        {
    
            InitializeComponent();
    
        }
    
    
    
        private void button1_Click(object sender, EventArgs e)
        {
    
            TextAvailable("From Form2");
    
        }
    
    }
    Hope this works

    Nagarjuna Dilip
    Friday, November 6, 2009 4:40 AM
  • XP, VS2008, C#

    I tried the first option you show and it did not work. Still loads Form2 in the top left of the desktop.

                Form2 child = new Form2();
                child.Owner = this;
                child.StartPosition = FormStartPosition.CenterParent;
    
                child.Show();
    

    I tried the second option and it does center in the center of the desktop.
    - Could the reason the CenterParent is not working is because I am using the debug run option in VS?

    Friday, November 6, 2009 4:50 AM
  •           
                 Form2 child = new Form2();
                  //IF u use owner user this combination
                child.Owner = this;
                child.StartPosition = FormStartPosition.CenterScreen;
                
                //If u use parent use this combination
                child.Parent=this;
                child.StartPosition = FormStartPosition.CenterParent;
    
                child.Show();
    
    


    Nagarjuna Dilip
    Friday, November 6, 2009 5:10 AM
  • changed to   child.Parent = this; 

                Form2 child = new Form2();
                child.Parent = this;
                child.StartPosition = FormStartPosition.CenterParent;
    
                child.Show();
    



    and when I run it breaks and gives me the following error: 
    "ArgumentException was unhandled"
    "Top-level control cannot be added to a control."

    Just tried adding a MdiContainer and this did not work either.

            public Form1()
            {
                this.IsMdiContainer = true;
    
                InitializeComponent();
            }
    

     

    • Edited by ReggieH Friday, November 6, 2009 5:50 AM add info
    Friday, November 6, 2009 5:38 AM
  • Hi,


                Form2 child = new
     Form2();
    child.mdiParent = this ;
    child.StartPosition = FormStartPosition.CenterParent;
    child.Show();

    Nagarjuna Dilip
    Friday, November 6, 2009 5:55 AM
  • Hi,

    You can use this code. You don't have to use mdiparent.

                Form1 f1 = new Form1();
                f1.Show();
                int x=this.DesktopBounds.Left + (this.Width-f1.Width)/2;
                int y=this.DesktopBounds.Top + (this.Height-f1.Height)/2;
                f1.SetDesktopLocation(x, y);
    • Marked as answer by ReggieH Friday, November 6, 2009 3:59 PM
    Friday, November 6, 2009 7:56 AM
  • Thanks Tamer this works, and thanks to all who replyed. I have listed the final code below for anyone else that wants to use this.
    --- Form1 ---
    namespace ParentChildTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 child = new Form2();
    
                child.Owner = this;
    
                child.Show();
    
                int x = this.DesktopBounds.Left + (this.Width - child.Width) / 2;
                int y = this.DesktopBounds.Top + (this.Height - child.Height) / 2;
                child.SetDesktopLocation(x, y);
            }
        }
    }
    
    --- Form2 ---
    namespace ParentChildTest
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                this.Owner.Enabled = false;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.Owner.Enabled = true;
                this.Close();
            }
        }
    }
    
    Friday, November 6, 2009 4:05 PM