In C# i want to create a panel that has the properties of a MDI container ie. isMdiContainer = true.
I tried something like this
form.MDIParent = thi
I used Mathias's answer above and was able to get rid of most of the issues raised in the comments. I created a helper class for the child forms as well, in case anyone wanted to use it and/or make it better.
public class MdiClientPanel : Panel
{
private MdiClient _ctlClient = new MdiClient();
// Callback event when a child is activated
public delegate void ActivateHandler(object sender, MdiClientForm child);
public event ActivateHandler OnChildActivated;
///
/// The current active child form
///
public Form ActiveMDIWnd
{
get;
set;
}
///
/// List of available forms
///
public List ChildForms = new List();
///
/// Std constructor
///
public MdiClientPanel()
{
base.Controls.Add(_ctlClient);
}
private Form _mdiForm = null;
public Form MdiForm
{
get
{
if (_mdiForm == null)
{
_mdiForm = new Form();
// Set the hidden _ctlClient field which is used to determine if the form is an MDI form
System.Reflection.FieldInfo field = typeof(Form).GetField("ctlClient",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
field.SetValue(_mdiForm, _ctlClient);
}
return _mdiForm;
}
}
private void InitializeComponent()
{
SuspendLayout();
ResumeLayout(false);
}
///
/// Add this Form to our list of children and set the MDI relationship up
///
/// The new kid
/// The new kid
public MdiClientForm AddChild(MdiClientForm child)
{
child.MyMdiContainer = this;
child.MdiParent = MdiForm;
ChildForms.Add(child);
return child;
}
///
/// The user sent a "restore" command, so issue it to all children
///
public void RestoreChildForms()
{
foreach (MdiClientForm child in ChildForms)
{
child.WindowState = FormWindowState.Normal;
child.MaximizeBox = true;
child.MinimizeBox = true;
}
}
///
/// Send the Activated message to the owner of this panel (if they are listening)
///
/// The child that was just activated
public void ChildActivated(MdiClientForm child)
{
ActiveMDIWnd = child;
if (OnChildActivated != null)
OnChildActivated(this, child);
}
///
/// The child closed so remove them from our available form list
///
/// The child that closed
public void ChildClosed(MdiClientForm child)
{
ChildForms.Remove(child);
}
}
///
/// A wrapper class for any form wanting to be an MDI child of an MDI Panel
///
public class MdiClientForm : Form
{
///
/// My parent MDI container
///
public MdiClientPanel MyMdiContainer { get; set; }
///
/// Standard Constructor
///
public MdiClientForm()
{
Activated += OnFormActivated;
FormClosed += OnFormClosed;
}
///
/// Reports back to the container when we close
///
void OnFormClosed(object sender, FormClosedEventArgs e)
{
MyMdiContainer.ChildClosed(this);
}
///
/// Reports back to the parent container when we are activated
///
private void OnFormActivated(object sender, EventArgs e)
{
MyMdiContainer.ChildActivated(this);
}
}