2009-08-09 14 views
2

Comment puis-je ouvrir une seconde fenêtre directement au-dessus de la première, et pas légèrement en diagonale vers le bas ou vers la droite à la position par défaut? J'essaie juste de faire quelques clics pour être cliqué. Y a-t-il un autre moyen de le faire? Pourquoi ne pas CenterParent le faire? Que fait CenterParent alors?Windows.Forms Visual Studio, Comment ouvrir une seconde fenêtre directement sur la première fenêtre?

+0

Le parent central fonctionne lorsque vous affectez le parent du second formulaire. Vous pouvez le faire dans le constructeur de la deuxième forme. –

+0

Question similaire: http://stackoverflow.com/questions/944897/show-a-child-form-in-the-centre-of-parent-form-in-c/944919 –

Répondre

2

Essayez de définir l'emplacement du nouveau formulaire comme étant égal au premier formulaire existant. Assurez-vous que la propriété StartPosition du deuxième formulaire est définie sur "Manual". Cela suppose que vos formulaires sont tous les mêmes tailles.

constructeur exemple de la forme « flottante »:

// reference to the form underneath, as it might 
// change location between creating the FloatingWindow, and showing 
// FloatingWindow! 
Form BeneathWindow; 

public FloatingWindow(Form BeneathWindow) 
{ 
    InitializeComponent(); 

    // save this for when we show the form 
    this.BeneathWindow = BeneathWindow; 
    StartPosition = FormStartPosition.Manual; 

} 

// OnLoad event handler 
private void FloatingWindowLoad(object sender, EventArgs e) 
{ 
    Location = BeneathWindow.Location; 
} 

Si vos formulaires sont pas les mêmes tailles, alors vous voudrez probablement les centrer. Vous pouvez utiliser CenterParent comme d'autres l'ont suggéré, ou vous pouvez manuellement les centrer vous, comme je l'aime parfois faire:

Location = new Point((BeneathWindow.Width - Width)/2 , (BeneathWindow.Height - Height)/2); 

Ou devrait fonctionner!

+1

Droit, mauvais endroit. Cela appartient (uniquement) à l'événement Load. –

+0

@ Henk: C'est vrai! Il est possible que la fenêtre en dessous change d'emplacement après la construction de la fenêtre ci-dessus. Post édité pour refléter cela. –

1

Voir les propriétés:

  1. StartPosition, essayez set à CenterParent

  2. propriétaire, essayez le mettre à ParentForm. Ou Ouvrir la fenêtre Yours en utilisant des méthodes:

    // 
    // Summary: 
    //  Shows the form with the specified owner to the user. 
    // 
    // Parameters: 
    // owner: 
    //  Any object that implements System.Windows.Forms.IWin32Window and represents 
    //  the top-level window that will own this form. 
    // 
    // Exceptions: 
    // System.ArgumentException: 
    //  The form specified in the owner parameter is the same as the form being shown. 
    public void Show(IWin32Window owner); 
    

Ou

// 
    // Summary: 
    //  Shows the form as a modal dialog box with the specified owner. 
    // 
    // Parameters: 
    // owner: 
    //  Any object that implements System.Windows.Forms.IWin32Window that represents 
    //  the top-level window that will own the modal dialog box. 
    // 
    // Returns: 
    //  One of the System.Windows.Forms.DialogResult values. 
    // 
    // Exceptions: 
    // System.ArgumentException: 
    //  The form specified in the owner parameter is the same as the form being shown. 
    // 
    // System.InvalidOperationException: 
    //  The form being shown is already visible.-or- The form being shown is disabled.-or- 
    //  The form being shown is not a top-level window.-or- The form being shown 
    //  as a dialog box is already a modal form.-or-The current process is not running 
    //  in user interactive mode (for more information, see System.Windows.Forms.SystemInformation.UserInteractive). 
    public DialogResult ShowDialog(IWin32Window owner); 

Vous pouvez aussi le faire par programme:

public partial class ChildForm : Form 
{ 
    public ChildForm(Form owner) 
    { 
     InitializeComponent(); 
     this.StartPosition = FormStartPosition.Manual; 
     int x = owner.Location.X + owner.Width/2 - this.Width/2; 
     int y = owner.Location.Y + owner.Height/2 - this.Height/2; 
     this.DesktopLocation = new Point(x, y); 
    } 
} 

formulaire parent:

public partial class ParentForm : Form 
{ 
    public ParentForm() 
    { 
     InitializeComponent(); 
    } 

    private void ButtonOpenClick(object sender, EventArgs e) 
    { 
     ChildForm form = new ChildForm(this); 
     form.Show(); 
    } 
} 
+0

myDialog.ShowDialog (this); travaillé pour moi (où c'est le formulaire parent). Merci. –

Questions connexes