Click here to Skip to main content
15,860,861 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
Colleagues,

What I'm trying to achieve is kind of an undocking behavior, where a control can undock from one form and float in a different form. My code is based on a working reference design* - DockExtender[^] **. Unfortunately, I ran into a problem. I’m getting an exception when I try to assign a different parent form to the control.

C#
private void FloatPanel()
{
   // this method resides in the new parent form for the control to float in
   // m_ctrlContainer control, which will be swapped into a floating form
   m_ctrlContainer.Parent = this;   // throws exception "Collection is read only."
}


The same exception occurs if I call this.Controlls.Add(m_ctrlContainer). It looks like the parent is in a state where controls can’t be added. What can I do to add controls to it?

Any suggestion, insight or reference is really appreciated!

- Nick

* EDIT: I'm re-creating the DockExtender (reference design). It's an exercise rather than attempt to reuse a docking framework as-is.

** EDIT: Parallel thread in the DockExtender article discussion[^].
Posted
Updated 31-Jan-11 8:33am
v4

My m_ctrlContainer was a "built-in" panel of a SplitContainer (as in splitContainer1.Panel1). That's why the collection was read-only. Well duh.

This code had pointed me in the right direction.
C#
m_ctrlContainer.Parent.Controls.Remove(m_ctrlContainer);  // threw exception "Collection is read only."
this.Controls.Add(m_ctrlContainer);  // although intuitively, I was expecting the exception here 


Subsequently, I've replaced the SplitContainer with separate splitter and panels. The hierarchy, obviously, now looks like this:
Form1
  - panel1
  - splitter1
  - panel2

That made explicit un-parenting and re-parenting work. Implicit un-parenting works as well.
C#
m_ctrlContainer.Parent = this;  // Probably, the framework is calling .Parent.Controls.Remove() and this.Controls.Add() internally.

Thanks to all for your support and advice!

- Nick
 
Share this answer
 
v2
Comments
AspDotNetDev 2-Feb-11 13:23pm    
I'm glad you figured it out, and thanks for posting the answer here!
Don't know if it will help, but have you looked at SDock[^] on CodePlex. You'll probably not like that it's Japanese, so you'll have to fix localization. Looks very nice though :)

Personally I use DevExpress XtraBars[^] for this kind of functionality.

Update
Try adding a level of indirection - don't use the form, but a panel on that form as the new parent.

Regards
Espen Harlinn
 
Share this answer
 
v3
Comments
Nick Alexeev 30-Jan-11 16:39pm    
I can add Weifen Luo's DockManager ( http://sourceforge.net/projects/dockpanelsuite ) to the list of noteworthy OpenSource docking frameworks. At the same time, I'm re-creating the DockExtender project as an exercise rather than an attempt to use DockExtended as-is.
Espen Harlinn 30-Jan-11 16:49pm    
DockPanel Suite - looks nice :)
Sergey Alexandrovich Kryukov 30-Jan-11 19:12pm    
Nick, I voted 5 for your answer (perhaps, I'll need more of your code to see what's going on, because I experimented with assignments to Parent, and it worked well for me).
As to Weifen Luo's Dock Manager, yes, I successfully used it, but I found good amount of bugs I had to work around, also, I had to re-write whole level used for persisting of layout -- Weifen's approach is tedious and I consider it unusable). In other word, the library work well only if you limit its use to certain discipline.
Do you plan to publish your work? Would be interesting to see...
--SA
Nick Alexeev 2-Feb-11 17:22pm    
There's some chance that it will be published.
DockExtender ( http://www.codeproject.com/KB/miscctrl/DockExtender.aspx ) is a significant inspiration to my project. Right now, I'm thinking of inviting Herre Kuijpers, who wrote the DockExtender, as a co-author for this new article.

- Nick
Sergey Alexandrovich Kryukov 2-Feb-11 18:08pm    
Great, Nick,
You probably understand: if you do this work well and publish an article and other documentation is an attractive and helpful way this work can become very popular.
Is if WinForms only, no WPF (I would be more interested in WPF)?
Are you going to invite to contribute?
--SA
Perhaps you need to remove the control from its old parent first? Something like:
C#
m_ctrlContainer.Parent.Controls.Remove(m_ctrlContainer);
this.Controls.Add(m_ctrlContainer);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900