FormStartPosition.CenterParent does not work

前端 未结 13 1024
独厮守ぢ
独厮守ぢ 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:59

    JYelton's answer worked for me, but the form is only centered the first time Show() is called. If you want to Hide() the form, and then have it re-centered on the parent every time Show() is called you need use the following in your form:

    public new void Show(IWin32Window owner)
    {
        base.Show(owner);
    
        if (Owner != null)
            Location = new Point(Owner.Location.X + Owner.Width / 2 - Width / 2,
                Owner.Location.Y + Owner.Height / 2 - Height / 2);
    }
    

提交回复
热议问题