0

I have a form called frmTest1 with a splitcontainer with two panels. Panel 2 should load many forms one at a time. It works fine for the first form, but the second form cant then load a third form into panel 2 of frmTest1.

Here is stripped frmTest 1 code:

namespace Project1
{
public partial class frmMain3 : Form
{
    public frmMain3()
    {
        InitializeComponent();
    }

    private void btnShowTest1_Click(object sender, EventArgs e)
    {
        showScreen(new frmTest1());
    }

    public void showScreen(Control ctl)
    {
        while (splitContainer1.Panel2.Controls.Count > 0)
            splitContainer1.Panel2.Controls[0].Dispose();

        if (ctl is Form)
        {
            var frm = ctl as Form;
            frm.TopLevel = false;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.Visible = true;
        }
        ctl.Dock = DockStyle.Fill;
        splitContainer1.Panel2.Controls.Add(ctl);
    }
}
}

The second form's code is below:

namespace Project1
{
public partial class frmTest1 : Form
{
    public frmTest1()
    {
        InitializeComponent();
    }

    private void btnShowTest4_Click(object sender, EventArgs e)
    {

        frmMain3 main = new frmMain3();
        main.showScreen(new frmTest4()); //Nothing shows
    }
}
}

From the research I have done it seems the solution is to use a usercontrol but having never used it before, I am struggling. Can someone please show me how to resolve this?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mutongi Gava
  • 25
  • 1
  • 7
  • Does this answer your question? [How to Load Form inside panel other form in win app](https://stackoverflow.com/questions/17962341/how-to-load-form-inside-panel-other-form-in-win-app) – Sinatr Jan 27 '20 at 12:37
  • 2
    *"but having never used it before"* - is not a good excuse for not trying. Try. Maybe you will succeed on the first attempt. If not - you have a good (better than current) question and a reason to ask it on SO. – Sinatr Jan 27 '20 at 12:40
  • I have spent two days stuck on this already, thats why I asked – Mutongi Gava Jan 27 '20 at 12:45
  • You need to use an instance of the for. See my two form project : https://stackoverflow.com/questions/34975508/reach-control-from-another-page-asp-net – jdweng Jan 27 '20 at 12:52
  • The notes here may be interesting: [Auto-resize multiple windows forms being rendered on panel](https://stackoverflow.com/a/51476266/7444103). – Jimi Jan 27 '20 at 13:17
  • Simple mistake, add `main.Show();` to the Click event handler. – Hans Passant Jan 27 '20 at 14:02
  • Do not add forms to controls. It is meant to be the other way round. – TaW Jan 27 '20 at 15:41

1 Answers1

0

Please try to use a UserControl. Just change the base class of frmMain3 to "UserControl" and delete the whole "if (ctl is Form)"-part from showScreen().

Korny86
  • 33
  • 3