0

I have 2 forms, Form1.cs and Form2.cs. When I use this method:

var form= new Form2();
form.Show();

it opens me a new form, but I want to open inside the form1.cs, not another window with this form.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 2
    Would an MDI form be suitable for you? – ProgrammingLlama Feb 10 '21 at 08:06
  • 1
    Look at this documentation on how to create MDI parent and child forms: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-create-mdi-child-forms?view=netframeworkdesktop-4.8 – S2S2 Feb 10 '21 at 08:09

3 Answers3

1

You can add a panel control to your main form or to your form1 then add a button. Put this code in;

private void button1_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2() { TopLevel = false, TopMost = true };
    frm.FormBorderStyle = FormBorderStyle.Sizable;
    this.pContainer.Controls.Add(frm);
    frm.Show();
}

Please check this link https://foxlearn.com/windows-forms/how-to-add-the-form-in-panel-from-another-form-in-csharp-442.html

psy
  • 48
  • 5
  • As I understood, you wanna use UserControl and you can look at this tutorial https://www.youtube.com/watch?v=wZ63E_9ASwM –  Feb 10 '21 at 08:18
0

I have a simple solution as @John said you can use MDI form. Please check the code and the link below.

  protected void MDIChildNew_Click(object sender, System.EventArgs e)
  {
      Form2 newMDIChild = new Form2();
      // Set the Parent Form of the Child window.
      newMDIChild.MdiParent =this;
      // Display the new form.
      newMDIChild.Show();
  }

Note: Here is the link given Creating an MDI Child Form in C#

Srijon Chakraborty
  • 2,007
  • 2
  • 7
  • 20
0

You can assign Form1 as the owner of Form2 in Form.Show() method:

    var form = new Form2();
    form.Show(this); // assuming you are calling this method from Form1 (this = form1)