C# Panel As MDI Container

后端 未结 3 860
难免孤独
难免孤独 2021-01-14 02:44

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         


        
3条回答
  •  迷失自我
    2021-01-14 03:29

    It is possible to create an MDI-panel and show forms in that panel, something like the code below will do the job

    Mdi-Panel definiton:

    public class MdiClientPanel : Panel
    {
        private Form mdiForm;
        private MdiClient ctlClient = new MdiClient();
    
        public MdiClientPanel()
        {
            base.Controls.Add(this.ctlClient);
        }
    
        public Form MdiForm
        {
            get
            {
                if (this.mdiForm == null)
                {
                    this.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(this.mdiForm, this.ctlClient);
                }
                return this.mdiForm;
            }
        }
    }
    

    Usage:

    /// mdiChildForm is the form that should be showed in the panel
    /// mdiClientPanel is an instance of the MdiClientPanel
    myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm;
    

提交回复
热议问题