I would like to know if it is possible to show/place a form1 INSIDE form2. So that form1 is stuck inside form2.
Any help will be appreciated. thanks in advance.
Yes. The word you're looking for is MDI or Multiple Document Interface.
Just set form2's IsMdiContainer
property to true
and set form1's MdiParent
property to be form2 like so:
form2.IsMdiContainer = true;
form1.MdiParent = form2;
Of course, you cause the visual designer to write the first line for you by setting the property in the properties section:
EDIT
Here's an easy example.
Say you have 2 forms. FormContainer
and FormChild
.
FormContainer
is the application's main form.
All you need to do is make sure that FormContainer
's IsMdiContainer
property is set to true
and then you could add instances of other forms to this one by setting those instances' MdiParent
property. Except for the main form, any instance of the Form
class or a subclass is by default not visible.
public partial class FormContainer : Form {
public FormContainer() {
InitializeComponent();
this.IsMdiContainer = true;
// if you're not excited about the new form's backcolor
// just change it back to the original one like so
// note: The dark gray color which is shown on the container form
// is not it's actual color but rather a misterious' control's color.
// When you turn a plain ol' form into an MDIContainer
// you're actually adding an MDIClient on top of it
var theMdiClient = this.Controls
.OfType<Control>()
.Where(x => x is MdiClient)
.First();
theMdiClient.BackColor = SystemColors.Control;
}
private void FormContainer_Load(object sender, EventArgs e) {
var child = new FormChild();
child.MdiParent = this;
child.Show();
// if you wish to specify the position, size, Anchor or Dock styles
// of the newly created child form you can, like you would normally do
// for any control
child.Location = new Point(50, 50);
child.Size = new Size(100, 100);
child.Anchor = AnchorStyles.Top | AnchorStyles.Right;
}
}