How do you set the StartPosition of a Windows Forms form using code?

后端 未结 8 1587
夕颜
夕颜 2020-12-18 20:55

Is there a way to set the StartPosition of a Windows Forms form using code? It seems whatever I try results in the StartPostion being the default.

Here is what I am

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-18 21:26

    public DealsForm()
    {
        InitializeComponent();
        this.StartPosition = FormStartPosition.CenterParent;       
    }
    

    Try to put it before InitializeComponent(). It might be already too late after InitializeComponent (the form might be already launch and the StatPosition is set too late).

    Update

    I just wrote :

    public Form1()
    {
        InitializeComponent();
        this.StartPosition = FormStartPosition.CenterScreen;
    }
    

    And:

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 f = new Form1();
        f.Show();
    }
    

    In a VS project (brand new) and when I click in my form2 a button it open the form in the middle of the screen. You can do the same with Parent...

提交回复
热议问题