Search code examples
c#formsdocking

Form not showing on Show(), because it's being used(?)


I'm having trouble trying to remove a Form from this Dock Container once I used the dockContainer.Add(form) and later the dockContainer.Remove(dockableFormInfo) and when I make it show itself using form.Show(), it wont show up at all.

Looking trough the properties it shows that the form is actually visible = true, but even though the dockable control that uses the Form was removed from the Container, my thoughts are that it still used the Form resources and thus I am unable to make it appear outside the control.

How can I make the Form show up?


Solution

  • Problem solved, first thanks to Hans Passant for the TopLevel tip.

    After removing from the docking container, just set TopLevel to true!

    Here is the test code:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Crom.Controls.Docking;
    
    namespace DockingTester
    {
        public partial class Form1 : Form
        {
            private Form dummyForm;
            private readonly Guid dummyFormGuid = Guid.NewGuid();
    
    
            public Form1()
            {
                InitializeComponent();
                CreateDummyForm();
                this.dummyForm.Show();
            }
    
            private void CreateDummyForm()
            {
                dummyForm = new Form();
                this.dummyForm.Text = "Dummy docking test form";
            }
    
            private static void DockUndockForm(DockContainer dockContainer, Form form, Guid guid)
            {
                DockableFormInfo formInfo = dockContainer.GetFormInfo(guid);
    
                //Add
                if (formInfo == null)
                {
                    formInfo = dockContainer.Add(form, zAllowedDock.All, guid);
                    dockContainer.DockForm(formInfo, DockStyle.Left, zDockMode.Inner);
                }
                //Remove
                else
                {
                    Form dummy =  formInfo.DockableForm;
                    dockContainer.Undock(formInfo, new Rectangle(Point.Empty, new Size(100, 300)));
                    dockContainer.Remove(formInfo);
    
                    dummy.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
                    dummy.TopLevel = true;
                }
            }
    
            private void dockUndockButton_Click(object sender, EventArgs e)
            {
                if (this.dummyForm.IsDisposed)
                    CreateDummyForm();
                DockUndockForm(this.dockContainer1, this.dummyForm, this.dummyFormGuid);
            }
    
        }
    }