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

后端 未结 8 1537
夕颜
夕颜 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:10

    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);
    }
    
    0 讨论(0)
  • 2020-12-18 21:11

    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

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

    To center on parent for the .Show call, this is what I had to do:

    childForm.Location = new Point(
        (parentForm.Location.X + parentForm.Width / 2) - (childForm.Width / 2), 
        (parentForm.Location.Y + parentForm.Height / 2) - (childForm.Height / 2));
    childForm.StartPosition = FormStartPosition.Manual;
    
    0 讨论(0)
  • 2020-12-18 21:15

    You can do this by calling this.CenterToParent() in the Form_Load event (when the parent is actually known). Don't call this in the Constructor because the parent it set when Show(form) is called.

    private void myForm_Load(object sender, EventArgs e)
    {
        CenterToParent();
    }
    

    I know this thread is old but it can be answered pretty easily so hopefully help others who come across it find the easy solution.

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

    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.

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

    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);
    }
    
    0 讨论(0)
提交回复
热议问题