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

后端 未结 8 1570
夕颜
夕颜 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...

    0 讨论(0)
  • 2020-12-18 21:34

    Maybe you are not alone. Maybe you are not insane. Read this (Microsoft Connect Customer Feedback):

    Windows Form StartPosition property only works for .ShowDialog method and not for .Show method

    Customer: "Windows Form StartPosition only works for .ShowDialog method and not for .Show method. Note: I have also attached simple code and images of the results."

    MS: "Unfortunately, we will not be able to fix this particular issue in a future release, as a fix here would be a breaking change to the behavior of WinForms 1, 1.1 and 2"

    0 讨论(0)
提交回复
热议问题