Show a child form in the centre of Parent form in C#

后端 未结 19 1045
不思量自难忘°
不思量自难忘° 2020-11-28 08:21

I create a new form and call from the parent form as follows:

loginForm = new SubLogin();   
loginForm.Show();

I need to display the chi

相关标签:
19条回答
  • 2020-11-28 08:54

    The setting of parent does not work for me unless I use form.ShowDialog();.

    When using form.Show(); or form.Show(this); nothing worked until I used, this.CenterToParent();. I just put that in the Load method of the form. All is good.

    Start position to the center of parent was set and does work when using the blocking showdialog.

    0 讨论(0)
  • 2020-11-28 08:56

    Try:

    loginForm.StartPosition = FormStartPosition.CenterParent;
    loginForm.ShowDialog(this);
    

    Of course the child for will now be a blocking form (dialog) of the parent window, if that isn't desired then just replace ShowDialog with Show..

    loginForm.Show(this);
    

    You will still need to specify the StartPosition though.

    0 讨论(0)
  • 2020-11-28 08:59

    You need this:

    Replace Me with this.parent, but you need to set parent before you show that form.

      Private Sub ÜberToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ÜberToolStripMenuItem.Click
    
            'About.StartPosition = FormStartPosition.Manual ' !!!!!
            About.Location = New Point(Me.Location.X + Me.Width / 2 - About.Width / 2, Me.Location.Y + Me.Height / 2 - About.Height / 2)
            About.Show()
        End Sub
    
    0 讨论(0)
  • 2020-11-28 09:01

    When launching a form inside an MDIForm form you will need to use .CenterScreen instead of .CenterParent.

    FrmLogin f = new FrmLogin();
    f.MdiParent = this;
    f.StartPosition = FormStartPosition.CenterScreen;
    f.Show();
    
    0 讨论(0)
  • 2020-11-28 09:01

    On the SubLogin Form I would expose a SetLocation method so that you can set it from your parent form:

    public class SubLogin : Form
    {
       public void SetLocation(Point p)
       {
          this.Location = p;
       }
    } 
    

    Then, from your main form:

    loginForm = new SubLogin();   
    Point p = //do math to get point
    loginForm.SetLocation(p);
    loginForm.Show();
    
    0 讨论(0)
  • 2020-11-28 09:05

    If any windows form(child form) is been opened from a new thread of Main window(parent form) then its not possible to hold the sub window to the center of main window hence we need to fix the position of the sub window manually by means of X and Y co-ordinates.

    In the properties of Subwindow change the "StartPosition" to be "Manual"

    code in main window

    private void SomeFunction()
    {
        Thread m_Thread = new Thread(LoadingUIForm);
        m_Thread.Start();
        OtherParallelFunction();
        m_Thread.Abort();
    }
    
    private void LoadingUIForm()
    {
        m_LoadingWindow = new LoadingForm(this);
        m_LoadingWindow.ShowDialog();
    }
    

    code in subwindow for defining its own position by means of parent current position as well as size

    public LoadingForm(Control m_Parent)
    {
       InitializeComponent();
       this.Location = new Point( m_Parent.Location.X+(m_Parent.Size.Width/2)-(this.Size.Width/2),
                                  m_Parent.Location.Y+(m_Parent.Size.Height/2)-(this.Size.Height/2)
                                );
    }
    

    Here the co-ordinates of center of parent is calculated as well as the subwindow is kept exactly at the center of the parent by calculating its own center by (this.height/2) and (this.width/2) this function can be further taken for parent relocated events also.

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