从设计器代码中删除子控件

时间:2017-07-20 10:28:01

标签: c# tabcontrol designer tabpage

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

有什么想法吗?

对于创作,我使用以下代码。

    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

2 个答案:

答案 0 :(得分:1)

您是否尝试过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);
    }
}

答案 1 :(得分:0)

基于@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);
    }