2012-07-21 49 views
0

無論主表單是最大化還是正常大小,我都想在主表單的右上角打開一個(未裝飾的)表單。 但無論我如何嘗試,我都無法將它打開到我想要的位置。在mainform右上角打開子表單

我發現,描述如何打開相對於形式的另一種控制形式的職位,但沒有工作之一:

How to display a Modal form in a position relative to the a control in the parent window (opener)

曾試圖尋找了幾個小時,現在上谷歌的解決方案,但要麼沒有答案(doubdfull)或我不搜索緊密的詞組合(更可能)。

任何人都可以請給我指向類似的問題,或幫助我如何實現我所希望的?

回答

0

嘗試類似的東西:

private void button1_Click(object sender, EventArgs e) 
     { 
      ChildForm win = new ChildForm(); 
      int screenHeight = Screen.PrimaryScreen.WorkingArea.Height; 
      int screenWidth = Screen.PrimaryScreen.WorkingArea.Width; 

      Point parentPoint = this.Location; 

      int parentHeight = this.Height; 
      int parentWidth = this.Width; 

      int childHeight = win.Height; 
      int childWidth = win.Width; 

      int resultX = 0; 
      int resultY = 0; 

      if ((parentPoint.X + parentWidth + childWidth) > screenWidth) 
      { 
       resultY = parentPoint.Y; 
       resultX = parentPoint.X - childWidth; 
      } 
      else 
      { 
       resultY = parentPoint.Y; 
       resultX = parentPoint.X + parentWidth; 
      } 

      win.StartPosition = FormStartPosition.Manual;     
      win.Location = new Point(resultX, resultY); 
      win.Show(); 
     } 
2

聲音對我,你應該用你錨定到頂部和右側,一個用戶控件是。但讓我們做一個表格工作。您需要連接其Load事件,以便在重新調整自身之後將其移至正確的位置。然後,您需要主窗體的LocationChanged和Resize事件,以便您可以將子窗體保留在正確的位置。

因此,與樣板Form 1和Form名稱的示例程序和Form1上的按鈕來顯示孩子看起來是這樣的:

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
     this.button1.Click += button1_Click; 
     this.Resize += this.Form1_Resize; 
     this.LocationChanged += this.Form1_LocationChanged; 

    } 

    Form child; 

    private void button1_Click(object sender, EventArgs e) { 
     if (child != null) return; 
     child = new Form2(); 
     child.FormClosed += child_FormClosed; 
     child.Load += child_Load; 
     child.Show(this); 
    } 

    void child_FormClosed(object sender, FormClosedEventArgs e) { 
     child.FormClosed -= child_FormClosed; 
     child.Load -= child_Load; 
     child = null; 
    } 

    void child_Load(object sender, EventArgs e) { 
     moveChild(); 
    } 

    void moveChild() { 
     child.Location = this.PointToScreen(new Point(this.ClientSize.Width - child.Width, 0)); 
    } 

    private void Form1_Resize(object sender, EventArgs e) { 
     if (child != null) moveChild(); 
    } 

    private void Form1_LocationChanged(object sender, EventArgs e) { 
     if (child != null) moveChild(); 
    } 

} 
0

我想你應該嘗試這樣的事:

private void buttonOpen_Click(object sender, EventArgs e) 
{ 
    Form2 frm2 = new Form2(); 
    frm2.Show(); 
    //"this" is the parent form 
    frm2.DesktopLocation = new Point(this.DesktopLocation.X + this.Width - frm2.Width, this.DesktopLocation.Y); 
} 

簡單,簡單,適用於我(對我而言)。