简体   繁体   中英

How to open a child form from another child form?

I am working on a Windows application. I have a MainForm (Parent) and several 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.

I use this codes to show childForm and close the previous childForm in MainForm.cs (ParentForm):

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. 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. But when I click on the button, childForm2 shows out of MainForm. How can I show it inside of 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:

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

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

To show a child form in the main form use the WeiFen Luo library. This control will make it easier to dock forms into your main form visual studio docking screens

Form with 3 forms inside:

在此处输入图片说明

Make sure the IsMdiContainter prop is 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
  2. DockState.Right docks form at the rightside of the dockPanel
  3. DockState.Top docks form at the topside of the dockPanel

for more option check the link This control will handel responsifnes of the docking forms and will handle allot of positioning calculations for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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