Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

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

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 doing in the form to display:

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

Here is what I am doing to display the form:

    private void nvShowDeals_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        DealsForm frm = new DealsForm();

        frm.DataSource = this.Deals;

        frm.Show(this);
    }

I have tried putting the following in each of the above methods, to no avail:

this.StartPosition = FormStartPosition.CenterParent;

If I set it via the Property Editor ... it works perfectly, but I would really like to do it via code.

Should be a no-brainer ... but for the life of me I can't seem to figure it out ... maybe I need more caffeine.

Update:

If I do a ShowDialog() and pass the parent it works ... but I really don't want to show it as a Dialog.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...