FormStartPosition.CenterParent does not work

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

    I had the same problem, I eventually went with this:

    protected override void OnActivated(EventArgs e) {
        if(this.Modal == false && this.StartPosition == FormStartPosition.CenterParent) {
            if(!(this.Owner is Form)) {
                // Center to the last form opened before this one
                int numforms = Application.OpenForms.Count;
                this.Owner = Application.OpenForms[numforms - 2];
            }
            this.CenterToParent();
            Application.DoEvents();
        }
        base.OnActivated(e);
    }
    

    Used as:

    MyForm form = new MyForm();
    form.Show(this); // with or without
    

    The main advantage is that it does what your colleagues expect it to do, without requiring any hack in the calling form.

提交回复
热议问题