I'm trying to add a form to a SplitContainer from a child form. I can do it from the forms parent using this.
Lockdown.MainForm form = new Lockdown.MainForm(true);
form.MdiParent = this;
form.TopLevel = false;
form.Dock = DockStyle.Fill;
this.splitContainer.Panel2.Controls.Add(form);
form.Show();
But I can't figure out how to do it from a child of the parent form.
Any help is appreciated.
Here's how I solved the problem. I passed a reference to the child form.
MessageBoxRegister register = new MessageBoxRegister(this);
register.ShowDialog();
I then saved the reference in a global variable.
Launcher launcher;
public MessageBoxRegister(Launcher launcher)
{
InitializeComponent();
this.launcher = launcher;
}
Then I could open the form into the splitContainer like this.
Lockdown.MainForm form = new Lockdown.MainForm(true);
form.MdiParent = launcher;
form.TopLevel = false;
form.Dock = DockStyle.Fill;
launcher.splitContainer.Panel2.Controls.Add(form);
form.Show();
Related
I am creating a C# .Net 4.8 Winform app with one MDI Form. This MDI Form has several panels and a split container.
When adding a childform I use the following code:
frm.Text = formTitleText;
frm.MdiParent = this;
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Dock = DockStyle.Fill;
panelDeskTop.Controls.Add(frm);
panelDeskTop.Tag = frm;
frm.BringToFront();
lblTitleChildForm.Text = $"{frm.Text}({this.MdiChildren.Length})";
frm.Show();
The form shows in the panel, but is not added as a MDI Child. The Parent (This) is the MDI Parent and has property of IsMDIContainer. So the this.MdiChildren.Length is always 0.
Not sure why?
Thanks in advance.
It's expected. You've set another parent for it: panelDeskTop.Controls.Add(frm);.
Consider the following points:
An MDI parent form has a control of type MdiClient. When you set MdiParent of a form, basically you are adding it to the controls collection of the MdiClient control.
When you ask MdiChildren of an MDI parent form, it returns the child forms of the MdiClient.
A form or a control, can have only one parent at time, and when you add it to controls collection of new parent, it will be removed from controls collection of the old parent.
Now I believe, it's clearer:
frm.MdiParent = this;
...
...
panelDeskTop.Controls.Add(frm);
The last line, removes the form from MdiChildren of the parent. That's why the array is empty.
Do you really need MDI?
It looks like you don't need MDI. If the child form is gonna fill the main panel without showing titlebar, then it basically means it doesn't need an mdi parent. Just make it a non-toplevel and add it to the controls collection of the main panel and show it.
Resizable sidebar for MDI parent
If you want have a resizable sidebar for MDI parent, then it's enough to Dock a panel and the splitter to left of the parenr, and then the right side will be occupied by the MdiClient. For example:
var mdiParent = new Form() { Size = new Size(700, 450), Text = "parent" };
mdiParent.Load += (obj, args) =>
{
mdiParent.IsMdiContainer = true;
mdiParent.Controls.Add(new Splitter()
{ Dock = DockStyle.Left, BackColor = Color.White, Width = 8 });
mdiParent.Controls.Add(new Panel()
{ Dock = DockStyle.Left, BackColor = Color.Black });
var child1 = new Form()
{ MdiParent = mdiParent, Text = "child1", Size = new Size(400, 300) };
var child2 = new Form()
{ MdiParent = mdiParent, Text = "child2", Size = new Size(400, 300) };
child1.Show();
child2.Show();
};
mdiParent.ShowDialog();
i'm developping a winforms application and i'm putting a mdi child form in splitcontainer.panel1.
when i want to close current mdi child to open another one i can't get the child form.
i'm using this code to open e new child but i want get the current child to close it :
Accueil accueil = new Accueil();
accueil.MdiParent = this;
accueil.TopLevel = false;
this.splitContainer1.Panel1.Controls.Add(accueil);
accueil.WindowState = FormWindowState.Maximized;
accueil.Size = this.splitContainer1.Panel1.ClientSize;
accueil.MinimizeBox = false;
accueil.MaximizeBox = false;
accueil.ControlBox = false;
accueil.Width = this.splitContainer1.Panel1.Width;
accueil.Height = this.splitContainer1.Panel1.Height;
accueil.Show();
Putting an MDI child window into a split container doesn't make any sense. You are turning the form into a plain control by setting its TopLevel property to false. Best not to lose the reference. But you will probably be ahead with:
while (splitContainer1.Panel1.Controls.Count > 0)
splitContainer1.Panel1.Controls[0].Dispose();
var accueil = new Accueil();
accueil.TopLevel = false;
accueil.Dock = DockStyle.Fill;
accueil.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
accueil.Visible = true;
this.splitContainer1.Panel1.Controls.Add(accueil);
Do consider using a UserControl instead, it is the sane approach with the least likely long-term confuzzlement.
I Create a Windows Forms application with C#.
I have a general Form and a panel on it.
I show subForm into this panel with code:
SubForm objForm= SubForm.InstanceForm();
this.IsMdiContainer = true;
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
now I want to show other form on subForm of this panel, But I dont know how to do it.
I think your problem resolved by this code:
SubForm objForm= SubForm.InstanceForm();
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
As I understand, you're very close. To add another form into subform try the same code instead:
pnlSubSystem.Controls.Add(objForm);
use (where objForm2 is the new subForm)
SubForm objForm2 = new SubForm();
objForm.Controls.Add(objForm2);
Since you already got the answer that by removing this.IsMdiContainer = true; your code would run perfectly fine. Because IsMdiContainer property changes the display and behavior of the form to an MDI parent form. When this property is set to true, the form displays a submerged client area. All MDI child forms assigned to the parent form are displayed within its client area.
SubForm objForm= SubForm.InstanceForm();
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
objForm form which will be the template for the child forms. Each time you want to create a new child window to your application, you can create a new instance of this template form and make the first form as its parent form.
//Create a new instance of the MDI child template form
SubForm objForm = new SubForm();
//Set parent form for the child window
objForm.MdiParent=this; // Last ObjForm or something
//Display the child window
objForm.Show();
Another way:
objForm.TopLevel = false;
objForm.Parent = pnlSubSystem;
objForm.Show();
This is my first answer on Stackoverflow.
I have a container MDI Form in C# with a 2 children in different sizes.
Is there a way when i initialize the new child to the container - i will progmatically set the size of the container to wrap the child form ?
when you opening new form from Mdi parent set its property size:
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Size = new Size(100, 100);//edit size
childForm.Show();
ok i found my answer thanks to IRSOG deleted answer:
Form form= new Form();
form.MdiParent = this;
this.Size = new Size(form.Width, form.Height);
form.Show();
hope it will help someone
I have three forms 1 is Mdiparent and the other two is simple form (Form1 and Form2).
Parent:
var frmForm1 = new Form1{ MdiParent = this};
frmForm1.Show();
Form1 :
var MParent = new Parent();
var frmForm2 = new Form2{ MdiParent = MParent};
frmForm2.Show();
My Problem is if i Show Form2 from Form1 it goes outside the MdiParent.
I just got the answer.
var frmForm2 = new Form2{ MdiParent =This.MParent};
frmForm2.Show();