将控件交换到其他父对象中 [英] Swapping control into a different parent
问题描述
同事,
我试图实现的是一种取消停靠的行为,其中控件可以从一种形式取消停靠并以另一种形式浮动.我的代码基于有效的参考设计*- DockExtender [
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."
}
如果我调用this.Controlls.Add(m_ctrlContainer)
,则会发生相同的异常.看起来父级处于无法添加控件的状态.我该怎么做才能向其中添加控件?
任何建议,见解或参考都非常感谢!
-尼克
*我正在重新创建DockExtender(参考设计).这是一种练习,而不是尝试按原样重用对接框架.
** DockExtender文章讨论中的并行线程 [ ^ ].
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
* I''m re-creating the DockExtender (reference design). It''s an exercise rather than attempt to reuse a docking framework as-is.
** Parallel thread in the DockExtender article discussion[^].
推荐答案
我的m_ctrlContainer
是SplitContainer的内置"面板(与splitContainer1.Panel1
相同).这就是为什么该集合是只读的.嗯,
这段代码为我指明了正确的方向.
Mym_ctrlContainer
was a "built-in" panel of a SplitContainer (as insplitContainer1.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
随后,我用单独的拆分器和面板替换了SplitContainer.显然,层次结构现在看起来像这样:
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
不知道是否有帮助,但是您是否看过 SDock [ ^ ]在CodePlex上.您可能会不喜欢它的日语,因此您必须修复本地化.虽然看起来很好看:)
我个人使用 DevExpress XtraBars [
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
也许您需要先从其旧父控件中删除该控件?像这样的东西:
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);
这篇关于将控件交换到其他父对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!