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

后端 未结 19 1043
不思量自难忘°
不思量自难忘° 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:46

    Make a Windows Form , then put option for it : CenterParent then use this Code :

    yourChildFormName x = new yourChildFormName();
    x.ShowDialog();
    
    0 讨论(0)
  • 2020-11-28 08:47
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
    
            CenterToParent();
        }
    
    0 讨论(0)
  • 2020-11-28 08:48

    As a sub form i think it's not gonna Start in the middle of the parent form until you Show it as a Dialog. .......... Form2.ShowDialog();

    i was about to make About Form. and this is perfect that's i am searching for. and untill you close the About_form you cant Touch/click anythings of parents Form once you Click for About_Form (in my case) .Coz its Showing as Dialog

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

    Why not use this?

    LoginForm.WindowStartupLocation = Windows.WindowStartupLocation.CenterOwner 
    

    (vb.net)

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

    It works in all cases, swap Form1 for your main form.

    Popup popup = new Popup();
    popup.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
    popup.Location = new System.Drawing.Point((Form1.ActiveForm.Location.X + Form1.ActiveForm.Width / 2) - (popup.Width / 2),(Form1.ActiveForm.Location.Y + Form1.ActiveForm.Height / 2) - (popup.Height / 2));
    popup.Show(Form1.ActiveForm);
    
    0 讨论(0)
  • 2020-11-28 08:51

    When you want to use a non-blocking window (show() instead of showDialog()), this not work:

    //not work with .Show(this) but only with .ShowDialog(this)
    loginForm.StartPosition = FormStartPosition.CenterParent;
    loginForm.Show(this);
    

    In this case, you can use this code to center child form before display the form:

    //this = the parent
    frmDownloadPercent frm = new frmDownloadPercent();
    frm.Show(this); //this = the parent form
    //here the tips
    frm.Top = this.Top + ((this.Height / 2) - (frm.Height / 2));
    frm.Left = this.Left + ((this.Width / 2) - (frm.Width / 2));
    
    0 讨论(0)
提交回复
热议问题