控件利用重写OnCreateControl事件,动态生成控件中的子控件,类似的代码如下(实际的控件创建了多个子控件)
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (DesignMode)
            {
                IDesignerHost host = (IDesignerHost)Site.GetService(typeof(IDesignerHost));
                if (!Controls.ContainsKey("pnlContent"))
                {
                    Panel c = host.CreateComponent(typeof(Panel), "pnlContent") as Panel;
                    c.BackColor = Color.Red;
                    c.Parent = this;
                }
            }
        }
控件运行和设计都很正常,但是在从winform上删除控件时就没有反应了CPU占用100%,实在是不知道哪儿出问题了,我用了一个简单的例子测试也没出现上述情况,帮我看看可能会在哪儿出问题呢

解决方案 »

  1.   

    楼主发个DEMO给我吧。。另外,在可能没有释放控件的地方应该调用DestroyComponent来把生成的控件清除。。
    如果觉得调试有困难,不妨使用Microsoft CLR Debugger来进行调试。。
    写控件是一件很麻烦的工作,不过也能学会很多,楼主多加把劲。。
      

  2.   

    多谢大家,终于打到问题所在了。
    其中一个类中有对另一个类引用的属性,我把这个属性不赋值就没问题了,不过真不知道是为什么呢。
            protected override void OnCreateControl() 
            { 
                base.OnCreateControl(); 
                if (DesignMode) 
                { 
                    IDesignerHost host = (IDesignerHost)Site.GetService(typeof(IDesignerHost)); 
                    if (!Controls.ContainsKey("pnlContent")) 
                    { 
                        Panel c = host.CreateComponent(typeof(Panel), "pnlContent") as Panel; 
                        c.BackColor = Color.Red; 
                        c.ParentPanel=this;//就是在这个地方出的问题把这个地方注释掉立马没问题了
                        c.Parent = this; 
                    } 
                } 
            } 
      

  3.   

    可能与.net垃圾收集机制有关吧,你虽然删除了,但是还有引用的,所以不会释放资源