FormStartPosition.CenterParent does not work

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

    I'm using this code inside my main form, hope it helps:

    var form = new MyForm();
    form.Show();
    if (form.StartPosition == FormStartPosition.CenterParent)
    {
        var x = Location.X + (Width - form.Width) / 2;
        var y = Location.Y + (Height - form.Height) / 2;
        form.Location = new Point(Math.Max(x, 0), Math.Max(y, 0));
    }
    

提交回复
热议问题