tags:

views:

930

answers:

7

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.

A: 

My first reaction is: experiment a bit with VS2008. It should be in the general properties screen.

If you don't have Visual Studio, then it gets a little harder.

A good site to check might be this one: csharp-online.net

Sorry that I can't be more helpfull

Vordreller
A: 

Did you try to set the property in the calling method?

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

    frm.DataSource = this.Deals;

    // Insert this
    frm.StartPosition = FormStartPosition.CenterParent;

    frm.Show(this);
}
splattne
I tried this ... doesn't work ... you would think something so simple would be simple to implement.
mattruma
A: 
public DealsForm()
{
    this.StartPosition = FormStartPosition.CenterParent;
    InitializeComponent();

}

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()
    {
        this.StartPosition = FormStartPosition.CenterScreen;
        InitializeComponent();
    }

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...

Daok
I tried this ... doesn't work ... you would think something so simple would be simple to implement.
mattruma
It's just a question of when the Property is called, pretty sure. I was going to help you and try a simple solution but you vote me down so forget my help. Have a nice day.
Daok
I didn't vote you down ... in fact ... I just up voted you to counter whomever down voted you.
mattruma
@Daok you are actually not gonna help someone because somebody downvoted you :|
vikramjb
Kinda not very motivate to help when you have start to help and you are trying to do a small little project on the side to find the solution and you get down voted.
Daok
By the way IT WORKS...
Daok
Hmmm ... I think the problem I am having is my parent is equal to null ... even though I am passing it in the Show().
mattruma
You question is "Is there a way to set the StartPosition of a Windows Forms form using code?" And the answer is yes and I show you the code. What is your question if it's not the one i quote from you?
Daok
A: 

I'd suggest checking your DealsForm.Designer.cs and removing the line that sets the StartPosition in there, then doing it as you are.

Alternatively, perhaps try setting it in the Load or Shown events of the form instead.

Jon Grant
A: 

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

mattruma
+3  A: 

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

That is correct since ShowDialog would set frm.Parent == nvShowDeals.Parent
Since you are using .Show() then frm.Parent == null thus FormStartPosition.CenterParent is ignored.

So to accomplish this function I would make the following changes:

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

//DealsForm_Load Event
private void DealsForm_Load(object sender, EventArgs e)
{
    this.Location = this.Owner.Location;  //NEW CODE
}

And Here I would make the following changes:

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

    frm.DataSource = this.Deals;
    frm.StartPosition = FormStartPosition.Manual; //NEW CODE
    frm.Show(this);
}
PersistenceOfVision
You are correct ... I think the only other time it would not be ignored is if it was and MDI child.
mattruma
+2  A: 

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"

splattne