从设计器代码中删除 Child 个控件

Delete Child Controls from designer code

我正在使用自定义 TabPages 创建自己的 TabControl。除了删除部分外,它进展顺利。当我将 TabControl 添加到设计器中的表单时,一切正常,添加了 2 个默认 TabPages 并绘制了控件。但是,当我从设计器中的表单中删除 TabControl 时,属于 TabControl.Controls 集合的 TabPages 不会从设计器代码中删除。他们只是失去了 Parent.

有什么想法吗?

我使用以下代码进行创建。

    public class CustomTabControlDesigner : ParentControlDesigner
{
    DesignerVerbCollection _fVerbs;
    public override DesignerVerbCollection Verbs
    {
        get
        {
            if (_fVerbs == null)
            {
                _fVerbs = new DesignerVerbCollection(new[] { new DesignerVerb("Add Tab", OnAdd), new DesignerVerb("Del Tab", OnDel) });
            }

            return _fVerbs;
        }
    }

    void OnAdd(object sender, EventArgs e)
    {
        IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
        if (designerHost != null)
        {
            WTabPage newPage = (WTabPage)designerHost.CreateComponent(typeof(WTabPage));
            //newPage.Text = newPage.Name;

            ((WTab)Component).Controls.Add(newPage);
        }
    }
    void OnDel(object sender, EventArgs e)
    {
        IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
        if (designerHost != null)
        {
            ((WTab)Component).Controls.Remove(((WTab)Component).SelectedTab);
        }
    }

    public override void InitializeNewComponent(IDictionary defaultValues)
    {
        base.InitializeNewComponent(defaultValues);

        for (int i = 0; i < 2; i++)
        {
            OnAdd(this, EventArgs.Empty);
        }
    }
    //protected override void Dispose(bool disposing)
    //{
    //    for (int i = ((WTab)Component).Controls.Count - 1; i >= 0; i--)
    //    {
    //        ((WTab)Component).Controls.Remove(((WTab)Component).Controls[i]);
    //    }

    //    base.Dispose(disposing);
    //}

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        // Selection of tabs via mouse
        if (m.Msg == 0x201/*WM_LBUTTONDOWN*/)
        {
            WTab control = (WTab)Component;

            int lParam = m.LParam.ToInt32();

            Point hitPoint = new Point(lParam & 0xffff, lParam >> 0x10);

            if (Control.FromHandle(m.HWnd) == null) // Navigation
            {
                if (hitPoint.X < 18 && control.SelectedIndex > 0) // Left
                {
                    control.SelectedIndex--;
                }

                else
                {
                    control.SelectedIndex++; // Right}
                }
            }
            else
            {
                // Header click
                for (int i = 0; i < control.TabCount; i++)
                {
                    if (control.GetTabRect(i).Contains(hitPoint))
                    {
                        control.SelectedIndex = i;

                        return;
                    }
                }
            }
        }
    }
    protected override void OnDragDrop(DragEventArgs de)
    {
        ((IDropTarget)((WTab)Component).SelectedTab).OnDragDrop(de);
    }
    protected override void OnDragEnter(DragEventArgs de)
    {
        ((IDropTarget)((WTab)Component).SelectedTab).OnDragEnter(de);
    }
    protected override void OnDragLeave(EventArgs e)
    {
        ((IDropTarget)((WTab)Component).SelectedTab).OnDragLeave(e);
    }
    protected override void OnDragOver(DragEventArgs de)
    {
        ((IDropTarget)((WTab)Component).SelectedTab).OnDragOver(de);
    }
}

OnAdd 和 OnDel 由任务触发,而不是在添加或删除控件时触发:Task Img

你试过了吗IDesignerHost.DestroyComponent(...)

void OnDel(object sender, EventArgs e)
{
    IDesignerHost designerHost = (IDesignerHost) GetService(typeof(IDesignerHost));
    if (designerHost != null)
    {
        var tab = ((WTab) Component).SelectedTab;
        ((WTab) Component).Controls.Remove(tab);
        designerHost.DestroyComponent(tab);
    }
}

基于@Pablo notPicasso 的回答,我使用了以下似乎有效的代码。这有什么不妥吗?

        protected override void Dispose(bool disposing)
    {
        IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
        if (designerHost != null)
        {
            for (int i = ((WTab)Component).Controls.Count - 1; i >= 0; i--)
            {
                //
                var tab = ((WTab)Component).Controls[i];

                //
                ((WTab)Component).Controls.Remove(tab);

                //
                designerHost.DestroyComponent(tab);
            }
        }

        base.Dispose(disposing);
    }