web-dev-qa-db-fra.com

Afficher la boîte de dialogue au centre de son parent

Cela a été un gâchis de montrer une boîte de dialogue au centre de sa forme parent. Voici une méthode pour afficher une boîte de dialogue.

Je positionne son parent au centre mais je n'arrive pas à centrer la boîte de dialogue

private void OpenForm(Object point, Object height, Object width)
{
    FormLoading frm = new FormLoading();
    Point temp = (Point)point;
    Point location = new Point(temp.X + (int)((int)width) / 2, 
                               temp.Y + (int)((int)height) / 2);
    frm.Location = location;
    frm.ShowDialog();
}

private void btnView_Click(object sender, EventArgs e)
{
    try
    {                    
        ThreadStart starter= delegate { OpenForm(currentScreenLocation, 
                                                 this.Height, this.Width); };
        Thread t = new Thread(starter);
        t.Start();
        ////// Some functionality here...
        t.Abort();
    }
    catch (Exception)
    {
    }
}
49
Tausif Khan

Vous voudrez peut-être vérifier le Form.StartPosition propriété.

http://msdn.Microsoft.com/en-us/library/system.windows.forms.form.startposition.aspx

quelque chose comme:

private void OpenForm(Form parent)
{
    FormLoading frm = new FormLoading();
    frm.Parent = parent;
    frm.StartPosition = FormStartPosition.CenterParent;
    frm.ShowDialog();
}

Cela nécessite bien sûr de définir le parent du formulaire.

94
Kornelije Petak
10
user2070102

De plus, si vous souhaitez configurer un emplacement arbitraire, vous pouvez utiliser ce

FormLoading frm = new FormLoading();
Point location = new Point(300, 400);
frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
frm.Location = location;
frm.ShowDialog();
4
DuyLuc
NewForm.Show();

NewForm.Top = (this.Top + (this.Height / 2)) - NewForm.Height / 2;
NewForm.Left = (this.Left + (this.Width / 2)) - NewForm.Width / 2;
2
Billy Xd

si vous créez un MessageBox personnalisé, vous pouvez simplement mettre ceci:

CenterToParent();

dans votre méthode MessageBox formload() personnalisée.

2
Mohsen K