Click here to Skip to main content
13,344,016 members (98,886 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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.

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 30-Jan-11 11:18am
Updated 31-Jan-11 9:33am
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
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:
  - panel1
  - splitter1
  - panel2

That made explicit un-parenting and re-parenting work. Implicit un-parenting works as well.
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
AspDotNetDev 2-Feb-11 13:23pm
I'm glad you figured it out, and thanks for posting the answer here!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

Espen Harlinn
Nick Alexeev 30-Jan-11 16:39pm
I can add Weifen Luo's DockManager ( ) 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 :)
SAKryukov 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...
Nick Alexeev 2-Feb-11 17:22pm
There's some chance that it will be published.
DockExtender ( ) 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
SAKryukov 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?
SAKryukov 30-Jan-11 19:06pm
Espen, I voted 5 just because this is a good reference to know and because OP could use it as an alternative to the Nick's design.
However, I think Nick's question is more simple and it deserves an answer.
See also my comment to Nick's above.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Perhaps you need to remove the control from its old parent first? Something like:

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180111.1 | Last Updated 4 Feb 2011
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100