2016-08-21 48 views
-1

enter image description here新的形式展現在C#

當我按一下按鈕從主窗體邊欄,我想說明的第二Form像一個側邊欄給我的第一形態的右邊緣。我怎樣才能做到這一點?

namespace testing 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Form2 form = new Form2(); 
      form.Show(); 
     } 
    } 
} 
+3

你是什麼意思*作爲滑動條出來*? –

+0

也許你的意思是側邊欄? –

+0

@RezaAghaei是邊欄 – Rafi

回答

1

你需要編寫MoveResize活動相適應的其他形式。如果兩者都能夠移動,總是粘在一起,則需要爲兩者編寫事件;試試看能不能創造一個無限循環;-)

下面是一個例子:

private void Form1_Move(object sender, EventArgs e) 
{ 
    // you may or may not need this flag 
    // you would set and clear in the form's constructor and at the end of the Load event. 
    if (loading) return; 
    placeForm2(); 
} 

private void Form1_Resize(object sender, EventArgs e) 
{ 
    placeForm2(); 
} 

public void placeForm2() 
{ 
    form2.Top = this.Top; 
    form2.Left = this.Left + this.Width; 

    int sw = Screen.FromControl(this).WorkingArea.Width; 
    int sh = Screen.FromControl(this).WorkingArea.Height; 

    if (form2.Right >= sw) form2.Left = this.Left - form2.Width; 
    if (form2.Bottom >= sh) form2.Top = sh - form2.Height; 
} 

一個呼叫只需添加到placeForm2功能,以您的按鈕點擊以及..!

請注意,當您接近右側屏幕邊框時,我將第二個窗體從左側移動到右側。這是可選的,當然..

0

一個簡單的辦法:

private void button1_Click(object sender, EventArgs e) 
    { 
     int frm1Width = this.Width; 
     int frm1Top = this.Top; 
     int frm1Left = this.Left; 
     int delta = 15; 

     Form2 frm2 = new Form2(); 

     frm2.Show(); 
     frm2.Top = frm1Top; 
     frm2.Left = frm1Left + frm1Width - delta; 

    } 
+0

不處理表單1移動或調整大小。 – khargoosh