Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 
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
Edited 31-Jan-11 9:33am
v4
Rate this: bad
good
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:
Form1
  - 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
  Permalink  
v2
Comments
AspDotNetDev at 2-Feb-11 13:23pm
   
I'm glad you figured it out, and thanks for posting the answer here!
Rate this: bad
good
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 Smile | :)
 
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
  Permalink  
v3
Comments
Nick Alexeev at 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 at 30-Jan-11 16:49pm
   
DockPanel Suite - looks nice :)
SAKryukov at 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 at 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
SAKryukov at 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
SAKryukov at 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.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Perhaps you need to remove the control from its old parent first? Something like:
m_ctrlContainer.Parent.Controls.Remove(m_ctrlContainer);
this.Controls.Add(m_ctrlContainer);
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 PIEBALDconsult 110
3 Richard MacCutchan 105
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 4 Feb 2011
Copyright © CodeProject, 1999-2014
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