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

Can anyone have work in saving a panel layouts? like there's a default view and there is a custom view of panel layout and it can saved, so if you open the application, the last view saved will be the panel layout view #700

Open
AronJake opened this issue Feb 12, 2024 · 3 comments
Labels

Comments

@AronJake
Copy link

No description provided.

@pmrochen
Copy link
Contributor

You can save your layout in your main Form's OnFormClosing this way:

protected override void OnFormClosing(FormClosingEventArgs e)
{
	base.OnFormClosing(e);

	if (!e.Cancel)
	{
		// Save to Windows registry -- you can also use file or any other Stream type
		using (MemoryStream stream = new MemoryStream())
		{
			dockPanel_.SaveAsXml(stream, System.Text.Encoding.UTF8);

			using (RegistryKey key = Registry.CurrentUser.CreateSubKey(Program.RegistryPath))
			{
				if (key != null)
					key.SetValue("DockPanelConfig", stream.ToArray());
			}
		}
	}
}

...an then restore it in OnLoad:

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

	bool configLoaded = false;
	using (RegistryKey key = Registry.CurrentUser.OpenSubKey(Program.RegistryPath))
	{
		if (key != null)
		{
			byte[] config = key.GetValue("DockPanelConfig", null) as byte[];
			if (config != null)
			{
				dockPanel_.SuspendLayout(true);

				try
				{
					using (MemoryStream stream = new MemoryStream(config, false))
					{
						dockPanel_.LoadFromXml(stream, delegate (string persistString)
						{
							// Map serialized type name to actual instance
							if (persistString == typeof(OutputForm).ToString())
								return outputForm_;
							else if (persistString == typeof(ErrorListForm).ToString())
								return errorListForm_;
							// Add the rest of your docking items here...
							else
								return null;
						});

						configLoaded = true;
					}
				}
				catch (SystemException)
				{
					configLoaded = false;
				}
				finally
				{
					dockPanel_.ResumeLayout(true, true);
				}
			}
		}
	}

	if (!configLoaded)
	{
		// Set the default layout
		outputForm_.Show(dockPanel_);
		errorListForm_.Show(dockPanel_);
		//...
	}
}

@lextm lextm added the question label Feb 12, 2024
@AronJake
Copy link
Author

@pmprochen, Thank you for helping out. I will try this

@AronJake
Copy link
Author

I have successfully save an xml file but when im trying to load the xml, it will show the layout in 0.1 seconds and close again, its like a white shadow but the layout from what i save is correct, I just need to load the dockcontent and controls inside it properly. Can anyone help me to fix this issue? Its a big help

private void SaveLayout(string fileName)
{
using (var stream = new FileStream(fileName, FileMode.Create))
{
dockPanel1.SaveAsXml(stream, Encoding.UTF8);
}
}

    private void LoadLayout(string fileName)
    {
        if (File.Exists(fileName))
        {
            // Check if the DockPanel is already initialized
            if (dockPanel1.DockWindows.Count > 0 || dockPanel1.Contents.Count > 0)
            {
                // Clear the existing layout
                dockPanel1.SuspendLayout();
                CloseAllPanels();
                dockPanel1.ResumeLayout(true);
            }

            // Load the new layout
            using (var stream = new FileStream(fileName, FileMode.Open))
            {

                dockPanel1.LoadFromXml(stream, DeserializeDockContent);
                
                //Debug.WriteLine("Read Me");
            }
        }
    }
    private IDockContent DeserializeDockContent(string persistString)
    {
        // Implement the deserialization logic for your specific dock content types
        // For example:
        if (persistString == typeof(Form4).ToString())
        {
            return new Form4();
        }
        else
        {
            // Handle other types if needed
            return null;
        }
    }
    
      private void CloseAllPanels()
    {
        List<DockContent> contentsToClose = new List<DockContent>();

        foreach (var panel in dockPanel1.Panes)
        {
            foreach (var content in panel.Contents)
            {
                contentsToClose.Add((DockContent)content);
            }
        }

        foreach (var content in contentsToClose)
        {
            content.DockHandler.Close();
        }
    }
    
    //this is how I create the dock contents panel.
    
     private void CreateDockablePanel7()
    {
        // Create a new dockable panel
        DockContent dockableContent = new DockContent();
        dockableContent.Text = "Pass Details";

      

        Panel passDetails = new Panel();
        passDetails.Dock = DockStyle.Fill;


       
        dockableContent.Controls.Add(passDetails);

        passDetails.BackgroundImage = Properties.Resources.pass_details; // Replace with the name of your image
        passDetails.BackgroundImageLayout = ImageLayout.Zoom;


        // Show the dockable panel inside the dockPanel

        dockableContent.Show(dockPanel1, WeifenLuo.WinFormsUI.Docking.DockState.DockTopAutoHide);

    }

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

No branches or pull requests

3 participants