Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Exception "ActiveContent must be one of the visible contents, or null if there is no visible content" #668

Open
ChristopherPH opened this issue Oct 25, 2021 · 1 comment

Comments

@ChristopherPH
Copy link

Hello,

I know this issue has been raised at least twice before now ( #226 #502 ), but I am experiencing this with the following steps:

  • The dockpanel is set up with all the content as HideOnClose
  • The initial dock layout has a floating window that was not initially docked
  • The floating window is then docked (via code or manually)
  • The layout is saved
  • The window is undocked (via code, manually, or via loading a different saved layout)
  • The layout is loaded

Below is a code sample that will reproduce the exception. I am using version 3.04 and 3.10.

I'm hoping that I'm doing something wrong and it isn't a bug, but any help or pointers to get though this would be very much appreciated.

Thanks!

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace DockPanelSuiteCrash
{
    public partial class Form1 : Form
    {
        List<DockContent> LocalContent = new List<DockContent>();

        DockContent contentForm = new CustomDockContent()
        {
            Text = "ContentForm",
            Dock = DockStyle.Fill,
            DockAreas = DockAreas.Document,
            ShowHint = DockState.Document,
            HideOnClose = true,
            BackColor = Color.Teal,
        };

        DockContent toolForm = new CustomDockContent()
        {
            Text = "ToolForm",
            Dock = DockStyle.Fill,
            DockAreas = DockAreas.DockLeft | DockAreas.DockRight | DockAreas.DockTop | DockAreas.DockBottom | DockAreas.Float,
            ShowHint = DockState.Float,
            HideOnClose = true,
            Size = new Size(150, 200),
            BackColor = Color.AntiqueWhite,
        };

        static VS2015LightTheme theme = new VS2015LightTheme();

        DockPanel dockPanel = new DockPanel()
        {
            Dock = DockStyle.Fill,
            Theme = theme,
            DocumentStyle = DocumentStyle.DockingSdi,
        };

        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(dockPanel);
            this.PerformLayout();
        }

        public class CustomDockContent : DockContent
        {
            protected override string GetPersistString()
            {
                return this.Text;
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            LocalContent.Add(toolForm);
            LocalContent.Add(contentForm);

            foreach (var content in LocalContent)
            {
                if (content.ShowHint == DockState.Float)
                {
                    //get dockpanel location on screen to be able to center the floatwindow
                    var location = dockPanel.PointToScreen(dockPanel.Location);

                    //center floatwindow in dockpanel
                    var floatBounds = new Rectangle(
                        (dockPanel.Width / 2) - (content.Width / 2) + location.X,
                        (dockPanel.Height / 2) - (content.Height / 2) + location.Y,
                        content.Width, content.Height);

                    //show floating window
                    content.Show(dockPanel, floatBounds);
                }
                else
                {
                    //show any other content
                    content.Show(dockPanel);
                }
            }

            dockPanel.SaveAsXml("floating.xml");

            toolForm.DockState = DockState.DockTop;

            dockPanel.SaveAsXml("docked.xml");

            LoadLayout("docked.xml"); //This succeeds
            LoadLayout("floating.xml");
            LoadLayout("docked.xml"); //This fails
        }

        void LoadLayout(string filename)
        {
            try
            {
                foreach (var content in LocalContent)
                    content.DockPanel = null;

                foreach (var window in dockPanel.FloatWindows.ToList())
                    window.Close();

                dockPanel.LoadFromXml(filename, (persist) => LocalContent.FirstOrDefault(x => x.Text == persist));
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message);
            }
        }
    }
}
@ChristopherPH
Copy link
Author

It seems as though this problem goes away if I recreate the DockContent inside LoadFromXml() instead of re-using the saved content.

When comparing the properties of the DockContent from before calling content.Show(dockPanel, floatBounds); to after setting content.DockPanel = null;, I noticed the IsFloat property was not reset back to false.

I was not able to directly set content.IsFloat = false; as setting the property called CheckDockState() which ended up returning DefaultDockState, which ended up returning the ShowHint, which was DockState.Float.

I have been able to work around the problem by temporarily setting the ShowHint to DockState.Unknown, setting IsFloat = false; then setting 'ShowHintback toDockState.Float`.

This seems to stop the exception from happening, from my initial testing.

                foreach (var content in LocalContent)
                {
                    if (content.ShowHint == DockState.Float)
                    {
                        content.ShowHint = DockState.Unknown;
                        content.IsFloat = false;
                        content.ShowHint = DockState.Float;
                    }

                    content.DockPanel = null;
                }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants