Click here to Skip to main content
15,071,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
According to this Page
Use form as subform[^]

i do exactly what the page describes
but in the state of having two subforms or more it doesn't work

suppose i have a MenuStrip having two toolStripMenuItem Page1 and Page2
when i click Page 1 a subForm1 is added but when i click Page2 nothing happens
because the SubForm2 Components Lies behind SubForm1 Components
How can i tell the panel to discard SubForm1 and include only SubForm2
here is the code
C#
private void page1ToolStripMenuItem_Click(object sender, EventArgs e)
      {
          Subform SubForm = new Subform();
          SubForm.FormBorderStyle = FormBorderStyle.None;
          SubForm.TopLevel = false;
          SubForm.ShowInTaskbar = false;
          SubForm.Show();
          SubForm.Dock = DockStyle.Fill;
          this.panel1.Controls.Add(SubForm);
      }

      private void page2ToolStripMenuItem_Click(object sender, EventArgs e)
      {
          subform1 SubForm = new subform1();
          SubForm.FormBorderStyle = FormBorderStyle.None;
          SubForm.TopLevel = false;
          SubForm.ShowInTaskbar = false;
          SubForm.Show();
          SubForm.Dock = DockStyle.Fill;
          this.panel1 = new Panel();
          this.panel1.Controls.Add(SubForm);
      }


Thanks
Posted
Updated 16-Dec-14 21:39pm
v2
Comments
BillWoodruff 17-Dec-14 4:15am
   
I hope OriginalGriff's answer will meet your needs. However, I think inserting a Form inside another Form, or inside some ContainerControl on a Form, is always a very bad idea.

It is like using a cannon to shoot a horsefly.

You can easily use UserControls, Panels, or another ContainerControl object, to host whatever Controls/Code/Classes are now hosted on the Forms you insert.

I encourage to re-think your design.

1 solution

No.,that isn't what your code is doing - you are trying to replace the existing Panel with a new version in your Page2 code:
C#
this.panel1 = new Panel();
this.panel1.Controls.Add(SubForm);

That doesn't do what you want, however - because the old panel is still in the Form.Controls collection, not the new one - so the new subform1 is never displayed.
Change your code to just empty the old panel first:
C#
panel1.Controls.Clear();
panel1.Controls.Add(SubForm);
And it should all work.

BTW: You don't need to specify this all the time - you only need it when there is confusion as to which version of two variables with the same name you mean:
C#
private string text = "hello";
private string otherText = "12345";
private void change(string text)
   {
   this.text = "Goodbye";  // explicitly references the class level variable
   text += " there";       // implicitly references the method parameter;
   otherText = text;       // implicitly references class level otherText and method parameter.
   }
   
v2
Comments
BillWoodruff 17-Dec-14 4:10am
   
+5 Oh, yeah.
oula alsheikh 17-Dec-14 4:13am
   
Thanks very much for your answer panel1.controls.Clear()
that's what i was looking for
i think the link page should include this information to be complete
about this and its use i know what you explained any way thanks for your clarification and interest
OriginalGriff 17-Dec-14 4:42am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900