简体   繁体   English

如何从另一个子窗体中打开一个子窗体?

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

I am working on a Windows application. 我正在使用Windows应用程序。 I have a MainForm (Parent) and several childForm. 我有一个MainForm(父母)和几个childForm。 There is a listview in MainForm that contains a childForm name list and by clicking on each name in the list, the relevant childForm shows and the previous ChildForm closes. MainForm中有一个列表视图,其中包含一个childForm名称列表,通过单击列表中的每个名称,将显示相关的childForm并关闭先前的ChildForm。

I use this codes to show childForm and close the previous childForm in MainForm.cs (ParentForm): 我使用以下代码显示childForm并关闭MainForm.cs(ParentForm)中的先前childForm:

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

CloseForm() is a method that checks, which childForm is runnig and closes it. CloseForm()是一种检查哪个childForm被运行并关闭的方法。 So far everything is good. 到目前为止,一切都很好。

In one of the childforms there is a Button. 在其中一个子窗体中有一个按钮。 When the user clicks on it, it should close this childForm and show another. 当用户单击它时,它应关闭此childForm并显示另一个。 But when I click on the button, childForm2 shows out of MainForm. 但是,当我单击按钮时,childForm2在MainForm中显示出来。 How can I show it inside of MainForm? 如何在MainForm中显示它?

My code in the button click event: 我的代码在按钮单击事件中:

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

You should set same MdiForm and call Close at the end: 您应该设置相同的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/ http://www.independent-software.com/weifenluo-dockpanelsuite-tutorial-cookbook/

To show a child form in the main form use the WeiFen Luo library. 要在主表单中显示子表单,请使用WeiFen Luo库。 This control will make it easier to dock forms into your main form visual studio docking screens 通过此控件,可以更轻松地将表单停靠到主表单Visual Studio停靠屏幕中

Form with 3 forms inside: 内有3种形式的表格:

在此处输入图片说明

Make sure the IsMdiContainter prop is true. 确保IsMdiContainter道具为true。

在此处输入图片说明

Example: 例:

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

 }

More docking options: 更多对接选项:

  1. DockState.Fill docks form in over the hole dockPanel DockState.Fill停靠在停靠孔的窗体中
  2. DockState.Right docks form at the rightside of the dockPanel DockState.Right停靠点形成在停靠点的右侧
  3. DockState.Top docks form at the topside of the dockPanel DockState.Top停靠点在停靠面板的顶部形成

for more option check the link This control will handel responsifnes of the docking forms and will handle allot of positioning calculations for you. 有关更多选项,请检查链接。此控件将处理对接表单的响应,并将为您处理定位计算的分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM