2016-11-17 186 views
-1

我正在使用Windows應用程序。我有一個MainForm(父)和幾個childForm。在MainForm中有一個列表視圖,其中包含一個childForm名稱列表,並通過單擊列表中的每個名稱,顯示相關的childForm,並關閉以前的ChildForm。如何從另一個孩子窗體打開孩子窗體?

我使用這個代碼,以顯示childForm和關閉在MainForm.cs(ParentForm)先前childForm:

CloseForms(); 
frm_draft = new frm_ShowDraft(); 
frm_draft.MdiParent = this; 
frm_draft.Show(); 

CloseForm()是用於檢查的方法,該childForm運行過程中出現並且關閉它。 到目前爲止,一切都很好。

在其中一個子表單中有一個按鈕。當用戶點擊它時,它應該關閉這個childForm並顯示另一個。但是當我點擊按鈕時,childForm2顯示出MainForm。我如何在MainForm中顯示它?

我在按鈕的點擊事件代碼:

this.close(); 
frm_c2 = new frm_child2(); 
frm_c2.MdiParent = new MainForm().ParentForm; /// Or this.MdiForm 
frm_c2.Show(); 
+0

如果這是'C#'爲什麼你使用'VB.NET'標籤? – Bugs

+0

看看[這裏](http://stackoverflow.com/questions/8566582/how-to-centerparent-a-non-modal-form/8566716#8566716)。 – dee

+0

@ Jinx88909 VB.NET標記? – saedbfd

回答

0

http://www.independent-software.com/weifenluo-dockpanelsuite-tutorial-cookbook/

要顯示在主窗體子窗體使用魏風羅庫。 這種控制將使其更容易對接的形式進入主窗體的Visual Studio對接屏幕

形成內3種形式:

enter image description here

確保IsMdiContainter道具是真的。

enter image description here

實施例:

public Form1() 
{ 
    InitializeComponent(); 

    Form2 f2 = new Form2(); // create new form 

    // dockPanel is an control from WeiFen Luo more info see the link 
    // dockPanel control is docked in your mainform. 
    // this will open Form2 in the dockPanel and align it left 
    f2.Show(dockPanel, DockState.DockLeft); 

} 

更多對接選項:

  1. DockState.Fill碼頭形成在空穴DockPanel中
  2. DockState.Right碼頭在DockPanel中
  3. DockState.Top碼頭的rightside形成在形成的DockPanel中

更多選項頂面檢查鏈接 這種控制將韓德爾的對接形式responsifnes並會爲您處理所有定位計算。

+0

是的,我設置IsMdiContainer = True – saedbfd

+0

如果您嘗試使用ShowDialog()而不是.Show(); –

+0

但我想在MainForm裏面顯示childForm。 ShowDialog()作爲父母而不是孩子開放。 – saedbfd

2

您應該設置相同的mdi窗體,並在年底關閉致電:

frm_c2 = new frm_child2(); 
frm_cLetter.MdiParent = this.MdiParent; 
frm_cLetter.Show(); 
this.Close(); 
+0

我的問題是childForm2顯示像父窗體而不是childForm。我如何顯示childForm2像MainForm裏面的childForm? – saedbfd

+0

把this.Close()後frm_cLetter.Show() – Shadowed

+0

我把this.Close()在最後。但顯示childForm2出MainForm但 – saedbfd

相關問題