2009-08-09 6 views
2

2番目のウィンドウを最初のウィンドウのすぐ上に置くことはできますが、斜めに右下に、または元の位置に戻ることはできません。私はちょうどクリックしていくつかの画面を作ろうとしている。私はこれをやるべき別の方法がありますか?Windows.Forms Visual Studio、最初のウィンドウの上に直接2番目のウィンドウを開く方法は?

CenterParentはなぜですか? CenterParentは何をしますか?

+0

Center parentは、2番目のフォームの親を割り当てるときに機能します。これは、2番目のフォームのコンストラクタで行うことができます。 –

+0

似たような質問: http://stackoverflow.com/questions/944897/show-a-child-form-in-the-centre-of-parent-form-in-c/944919 –

答えて

2

新しいフォームの場所を最初の既存のフォームと同じに設定してみてください。 2番目のフォームのStartPositionプロパティが 'Manual'に設定されていることを確認します。これはあなたのフォームがすべてと同じサイズであることを前提としています。 「浮動」形式の

例のコンストラクタ:

// 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; 
} 

フォームは、同じサイズていない場合、あなたはおそらくそれらを中央にしたいと思います。私は時々、好きなようにやって他の人が示唆しているとして、あなたはCenterParentを使用することができ、または手動で、自分でセンタリングすることができます

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

どちらかが動作するはずです!

+1

右のコード、間違った場所。これはLoadイベントにのみ属します。 –

+0

@ Henk:そうです!上記のウィンドウが構築された後、下のウィンドウが場所を変更する可能性があります。これを反映するように投稿が編集されました。 –

1

プロパティを参照してください:

  1. 開始位置を、それがCenterParent

  2. 所有者に設定してみてください、ParentFormに設定してみてください。またはオープンユアーズウィンドウメソッドを使用して:

    // 
    // 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); 
    

または

// 
    // 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); 

たり、プログラム的にそれを行うことができます。

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); 
    } 
} 

親フォーム:

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);私のために働いた(これは親のフォームである)。ありがとう。 –

関連する問題