简体   繁体   中英

C# MDI Form in Panel

I want to make a panel contain a MDI form. I want to put MainWin into a Panel ( progPanel ) that is in another form of DesktopApp. I have used this C# Panel As MDI Container

I cannot recode the MainWin without MDI because that would take forever and I would lose some functionality.

    Canvas.MainWin canvasForm = new Canvas.MainWin();
    MdiClientPanel mdiClientPan = new MdiClientPanel();
    canvasForm.MdiParent = mdiClientPan.MdiForm;
    progPanel.Controls.Add(mdiClientPan);


    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;
            }
        }
    }

I get the error Form cannot be both an MDI child and MDI parent on line canvasForm.MdiParent = mdiClientPan.MdiForm; . I have tried using DesktopApp.ActiveForm.MdiParent and changing Form in MdiClientPanel to Canvas.MainWin , but nothing is working. Any suggestions?

You cannot do this.

The error message should have been clear enough. So should have been the answer to the other question that you linked .

MDI has been considered obsolete for many years now. Users find it excessively confusing, and there are much better paradigms for unifying multiple windows within a single container. Consider, for example, a tab control. Users are already familiar with this, since every browser in existence uses tabs to display multiple web pages.

Yes, rewriting the application to avoid MDI will take time. But it is your only option, because your idea won't work. And there's an additional benefit to the user.

To be honest, I'm not really sure why you would want an MDI form inside of a panel in the first place. Consider a floating toolbox window as an alternative.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM