Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

winforms

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.

like image 796
mattruma Avatar asked Oct 22 '08 13:10

mattruma


People also ask

How do you change the size of a form programmatically?

Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it. You can set the Width and Height manually.

What is the default value of forms StartPosition property *?

Remarks. This enumeration is used by the StartPosition property of the Form class. It represents the different start positions of the form. The default start position is WindowsDefaultLocation .

What does the start position Property determine?

This property enables you to set the starting position of the form when it is displayed at run time. The form's position can be specified manually by setting the Location property or use the default location specified by Windows.


1 Answers

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"

like image 61
splattne Avatar answered Sep 17 '22 15:09

splattne