C# Winform MDI Form with Split Container - MdiChildren is empty - c#

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();

Related

Remove top bar on child form

Making an MDI form, i wish to remove the top bar on all child forms. Working on Visual studio and c#. Any idea how? Im clueless.
Here the properies of the child form:
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.WindowState = FormWindowState.Normal
Me.MinimizeBox = False
Me.MaximizeBox = False
Me.ControlBox = False
Me.ShowIcon = False
Me.ShowInTaskbar = False
Me.Dock = DockStyle.Fill
Option 1: *
As mentioned in this answer, you can add a MenuStrip control to your MDI parent form, set its Visible property to false, and you should be good to go. The MDI child forms won't have a title bar displayed as long as they are maximized.
Option 2: *
Set the MdiParent Property of the child form.
Set the FormBorderStyle property of the child form to FormBorderStyle.None.
Set the Dock property of the child form to DockStyle.Fill. Note: This must come after setting the MdiParent or else it won't work.
That's it, you don't need to change any other properties (WindowState, ControlBox, etc.). Just maintain the order of the steps above.
Here's an example:
private void OpenAndDockMdiChild()
{
Form2 childForm = new Form2();
childForm.MdiParent = this; // This must come **before** setting
// the `Dock` property.
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
childForm.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
OpenAndDockMdiChild();
}
Result:
Hope that helps.
* Tested with .NET 4.5.2 on both Windows 7 and Windows 10.

Adding a form to a splitcontainer from a child form

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();

How to Load Form inside panel other form in win app

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.

C# MDI form container wrap child form

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

Child Form Size Set to the MDI Form's MDI Container's Size

I'd like to know how I can have a child form load up with it's size covering the complete MDI Parent's MDI container space (the dark-gray thing)? Setting the child form's WindowState to Maximized is not an option as it'll maximize any other form too. Suggestions?
I think this code should do it:
Form childForm = new Form();
f.Left = 0;
f.Top = 0;
f.Size = ParentForm.ClientRectangle.Size;
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/a315cefe-fe7f-4e23-b3e0-6cf614501ac2/
check the link the issue is resolved by inheriting the size
I added a couple of lines to get it to fit inside the frame and it works well.
Form childForm = new Form();
childForm.Left = 0;
childForm.Top = 0;
Rectangle recNew = new Rectangle();
recNew = ParentForm.ClientRectangle;
recNew.Height -= 4;
recNew.Width -= 4;
childForm .Size = recNew.Size;
I hope that helps!

Categories