FormStartPosition.CenterParent does not work

前端 未结 13 979
独厮守ぢ
独厮守ぢ 2021-01-01 08:57

In the following code, only the second method works for me (.NET 4.0). FormStartPosition.CenterParent does not center the child form over its parent. Why?

13条回答
  •  粉色の甜心
    2021-01-01 09:57

    If you set the owner of the child form like so:

    Form2 f = new Form2();
    f.Show(this);
    

    You can then center it easily like this:

    Form2_Load(object sender, EventArgs e)
    {
        if (Owner != null)
            Location = new Point(Owner.Location.X + Owner.Width / 2 - Width / 2,
                Owner.Location.Y + Owner.Height / 2 - Height / 2);
    }
    

提交回复
热议问题