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.
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.
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
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#
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)