簡體   English   中英

如何從另一個子窗體中打開一個子窗體?

[英]How to open a child form from another child form?

我正在使用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();

您應該設置相同的MdiForm並在最后調用Close:

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

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

要在主表單中顯示子表單,請使用WeiFen Luo庫。 通過此控件,可以更輕松地將表單停靠到主表單Visual Studio停靠屏幕中

內有3種形式的表格:

在此處輸入圖片說明

確保IsMdiContainter道具為true。

在此處輸入圖片說明

例:

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停靠在停靠孔的窗體中
  2. DockState.Right停靠點形成在停靠點的右側
  3. DockState.Top停靠點在停靠面板的頂部形成

有關更多選項,請檢查鏈接。此控件將處理對接表單的響應,並將為您處理定位計算的分配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM