在设计模式下从表单中删除自定义控件后,清除自定义控件属性的InnerList元素

在设计模式下从表单中删除自定义控件后,清除自定义控件属性的InnerList元素

问题描述:

我将创建的自定义控件添加到新的Windows窗体中,并通过制表符"(Tabs)属性向其中添加一些选项卡.但是,当我从Windows窗体中删除自定义控件时,不会删除制表符"属性的元素.有关更多信息,请参见下图:
图1 [ ^ ]
图1-我的自定义控件"Tabs"属性及其集合编辑器"
1.蓝框:我的自定义控件的标签"属性
2.红框:在标签"属性中添加了元素
当我向Tabs属性添加一些成员时,将显示图1.
图2 [ ^ ]
图2-将一些成员添加到Tabs属性后,Windows Form项目控件
1.红框:我的自定义控件
2.蓝框:向Tabs属性添加了元素
图3 [ ^ ]
图3-从Windows窗体删除我的自定义控件后,Windows窗体项目控件
-红色框:向Tabs属性添加了元素
正如您在图2和3中看到的那样,我向Tabs属性添加了一些成员,并且从其父控件(Windows窗体)中删除了自定义控件之后,Tabs属性成员并未被删除.
我的Tabs属性与从CollectionBase类派生的Tabs类相关,并向其实现一些方法,如Add,Remove,Clear等.我在Dispose方法中调用Clear和RemoveRange方法,但是它不起作用.
我的代码如下:

I add my created custom control to the new Windows Form and add some Tab to it by "Tabs" property. However, when I delete my custom control from Windows Form, elements of "Tabs" property are not removed. Please see below figures for more information:
Figure 1[^]
Figure 1 - My custom control "Tabs" property with its Collection Editor
1. Blue Box: "Tabs" property of my custom control
2. Red Box: Added elements to the "Tabs" property
Figure 1 displays when I add some member to the Tabs property.
Figure 2[^]
Figure 2 - Windows Form project controls after add some member to the Tabs property
1. Red Box: My custom control
2. Blue Box: Added elements to the Tabs property
Figure 3[^]
Figure 3 - Windows Form project controls after delete my custom control from Windows Form
- Red Box: Added elements to the Tabs property
As you can see in the figure 2 and 3, I add some member to the Tabs property and after delete my custom control from its parent control (Windows Form), Tabs property members have not been removed.
My Tabs property is related to the Tabs class that derived from CollectionBase class and implement some methods such as Add, Remove, Clear and, etc. to it. And I call Clear and RemoveRange methods in the Dispose method, but it doesn''t work.
My codes as follows:

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
[ToolboxItem(true), ToolboxBitmap(typeof(ToolboxIconResourceFinder), "FloorsGrouping.bmp")]
[DisplayName("Floors Group")]
[Editor("WindowsFormsControlLibrary2.FloorsGrouping, WindowsFormsControlLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=197889249da45bfc", typeof(UITypeEditor))]
[Description("Floorssssssss")]
[Category("Saino")]
//[DefaultEvent("")]
[DefaultProperty("Text")]
[DesignerCategory("Component")] //Form //Designer //Empty String ("")
public partial class FloorsGrouping : Bar
{
    private static Font fntDefaultFont = SystemFonts.DefaultFont;
    private static string strDefaultAccessibleDescription = "";
    private static string strDefaultAccessibleName = "";

    private bool canDockBottom = false;
    private bool canDockTop = false;
    private bool fadeEffect = true;
    private int selectedDockTab = 0;
    private eDotNetBarStyle style = eDotNetBarStyle.StyleManagerControlled;
    private string text = "Floors Grouping";
    private FloorsInformation floorsInformation = new FloorsInformation();
    private Tabs tabs = new Tabs();
    private SupportedLanguages language = SupportedLanguages.English;
    private Styles groupingStyles = Styles.Classic;

    public FloorsGrouping()
    {
        InitializeComponent();
        ResetFont();
        ResetAccessibleDescription();
        ResetAccessibleName();
    }

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        Tab.Clear();
        Tab.RemoveRange(0, Tab.Count);
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private new void ResetFont()
    {
        Font = fntDefaultFont;
    }

    private new bool ShouldSerializeFont()
    {
        return !Font.Equals(fntDefaultFont);
    }

    [Category("Data")]
    [DisplayName("Tabs")]
    [Description("Tabsssssssssssss")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor(typeof(ItemsCollectionEditor), typeof(UITypeEditor))]
    //[Editor(typeof(ItemsCollectionEditor<SimilarFloorsInformation>), typeof(UITypeEditor))]
    //[Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
    public Tabs Tab
    {
        get
        {
            return tabs;
        }
    }

    [Category("Behavior")]
    [DisplayName("ContainerControl")]
    [Description("It indicates container control high lighter is bound to. It should be set to parent form.")]
    //[DefaultValue("")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Browsable(false), ReadOnly(true)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public Control ContainerControl
    {
        get { return hltMain.ContainerControl; }
        private set { hltMain.ContainerControl = this; }
    }

    protected override void OnParentChanged(EventArgs e)
    {
        ContainerControl = this;
    }
    protected override void OnCreateControl()
    {
        base.OnCreateControl();
    }

    protected override void OnControlRemoved(ControlEventArgs e)
    {
        //Tab.RemoveRange(0, tabs.Count);
        //Parent.Refresh();
        base.OnControlRemoved(e);
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
    }
}

[DisplayName("Floors Information")]
[Description("Floors Informationnnnnnnnnnnnnnnn")]
[DefaultProperty("Text")]
[DesignerCategory("Component")]
[ToolboxItem(false)]
public class FloorsInformation : DockContainerItem
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    private SimilarFloorsInformation similarFloorsInformation = new SimilarFloorsInformation();
    private AllFloorsInformation allFloorsInformation = new AllFloorsInformation();
    private string text = "Floors Information";

    public FloorsInformation()
    {
    }

    [Browsable(false), ReadOnly(true)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public new bool AutoCollapseOnClick
    {
        get { return base.AutoCollapseOnClick; }
    }

    [Browsable(false), ReadOnly(true)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public new Control Control
    {
        get { return base.Control; }
    }

    public new string Text
    {
        get
        {
            return text;
        }
        set
        {
            text = value;
        }
    }

    [Category("Data")]
    [DisplayName("Similar Floors Panel")]
    [Description("Similar Floors Panellllllllllllllllllll")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public SimilarFloorsInformation SimilarFloorsInfo
    {
        get
        {
            return similarFloorsInformation;
        }
        set
        {
            similarFloorsInformation = value;
        }
    }

    [Category("Data")]
    [DisplayName("All Floors Group")]
    [Description("All Floors Groupppppppppppppp")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public AllFloorsInformation AllFloorsInfo
    {
        get
        {
            return allFloorsInformation;
        }
        set
        {
            allFloorsInformation = value;
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
}

public class Tabs : CollectionBase
{
    public FloorsInformation this[int intIndex]
    {
        get
        {
            return (FloorsInformation)InnerList[intIndex];
        }

        set
        {
            InnerList[intIndex] = value;
        }
    }

    public int Add(FloorsInformation finfItemType)
    {
        return InnerList.Add(finfItemType);
    }

    public new void Clear()
    {
        InnerList.Clear();
    }

    public bool Contains(FloorsInformation finfItemType)
    {
        return InnerList.Contains(finfItemType);
    }

    public void Remove(FloorsInformation finfItemType)
    {
        InnerList.Remove(finfItemType);
    }

    public new void RemoveAt(int intIndex)
    {
        InnerList.RemoveAt(intIndex);
    }

    public void RemoveRange(int intIndex, int intCount)
    {
        InnerList.RemoveRange(intIndex, intCount);
    }

    public void Insert(int intIndex, FloorsInformation finfItemType)
    {
        InnerList.Insert(intIndex, finfItemType);
    }

    public void Reverse()
    {
        InnerList.Reverse();
    }

    public void Reverse(int intIndex, int intCount)
    {
        InnerList.Reverse(intIndex, intCount);
    }

    public int IndexOf(FloorsInformation finfItemType)
    {
        return InnerList.IndexOf(finfItemType);
    }

    public void AddRange(FloorsInformation[] finfItemType)
    {
        InnerList.AddRange(finfItemType);
    }

    public FloorsInformation[] GetValues()
    {
        FloorsInformation[] finfItemType = new FloorsInformation[InnerList.Count];
        InnerList.CopyTo(0, finfItemType, 0, InnerList.Count);
        return finfItemType;
    }

    protected override void OnInsert(int intIndex, object objValue)
    {
        base.OnInsert(intIndex, objValue);
    }

    protected override void OnClear()
    {
        base.OnClear();
    }
}

public class ItemsCollectionEditor : CollectionEditor
{
    private Type[] typItems;

    public ItemsCollectionEditor(Type typItem)
        : base(typItem)
    {
        typItems = new Type[] { typeof(FloorsInformation) };
    }

    protected override Type[] CreateNewItemTypes()
    {
        return typItems;
    }

    protected override CollectionForm CreateCollectionForm()
    {
        CollectionForm collectionForm = base.CreateCollectionForm();
        collectionForm.Text = "Tabs Collection Editor";
        return collectionForm;
        //return base.CreateCollectionForm();
    }
}


我想做一些类似DevComponents.DotNetBar的Bar控件的事情.有关更多信息,请参见以下内容:
图4 [ ^ ]
图4-在Windows窗体中添加Bar控件
1.左红色框:在Windows窗体中添加了Bar控件
2.右红色框:在Visual Studio的属性网格中添加了Bar控件. 当将Bar控件添加到Windows窗体时,Property Grid会发生变化,如图4所示.
图5 [创建Dock标签> ^ ]
图5-通过栏任务"选项创建Dock标签
-红色框:酒吧任务"下的创建Dock选项卡"选项
通过"Bar Tasks"下的"Create Dock Tab"选项,我们可以创建一个新的Dock Tab,以将新鲜的控件添加到Bar控件的特定选项卡中,如图5所示.
图6 [ ^ ]
图6-添加了Dock容器项及其相关的Panel Dock容器
1.红框:在Visual Studio的属性网格中添加了Bar控件. 2.左侧的蓝色框:在Bar控件中添加了Dock Container Item控件
3.右蓝框:在Visual Studio的属性网格中添加了Dock容器项控件. 4.左绿色框:已将面板Dock Container控件添加到Dock Container Item控件
5.右绿色框:在Visual Studio的属性网格中添加了面板停靠容器控件

图7
[^ ]
图7-添加了Dock容器项及其相关的Panel Dock容器
1.红框:请参见图6的红框
2.左蓝色框:在"Bar"控件中添加了"Dock Container Item"控件
3.右蓝框:在Visual Studio的属性网格中添加了Dock容器项控件. 4.右绿色框:在Visual Studio的属性网格中添加了面板停靠容器控件
每次在"Bar Tasks"下单击"Create Dock Tab"选项时,都会在DevComponents.DotNetBar的Bar控件中创建一个新的Dock Container Item控件及其相关的Panel Dock Container控件(请参见图6和7). /> 图8 [ ^ ]
图8-删除Bar控件后的Visual Studio属性网格
-红框:Visual Studio属性网格中的Windows窗体控件
从Windows窗体中删除Bar控件时,所有与其相关的控件也随之删除,并且仅保留Windows Form.
我想在自定义控件从其父控件(Windows窗体)中删除后自动删除标签"属性成员.


I want to do something like Bar controls of DevComponents.DotNetBar. Please see the following for more information:
Figure 4[^]
Figure 4 - Add Bar control to the Windows Form
1. Left Red Box: Added Bar control to the Windows Form
2. Right Red Box: Added Bar control in the Property Grid of Visual Studio
When Bar control is added to the Windows Form, Property Grid changes as you can see in the Figure 4.
Figure 5[^]
Figure 5 - Create Dock Tab by "Bar Tasks" option
- Red Box: "Create Dock Tab" option under "Bar Tasks"
With "Create Dock Tab" option under "Bar Tasks," we can create a new Dock Tab for add fresh controls to specific tab of Bar control as you can see in the Figure 5.
Figure 6[^]
Figure 6 - Added Dock Container Item and its related Panel Dock Container
1. Red Box: Added Bar control in the Property Grid of Visual Studio
2. Left Blue Box: Added Dock Container Item control to the Bar control
3. Right Blue Box: Added Dock Container Item control in the Property Grid of Visual Studio
4. Left Green Box: Added Panel Dock Container control to the Dock Container Item control
5. Right Green Box: Added Panel Dock Container control in the Property Grid of Visual Studio

Figure 7
[^]
Figure 7 - Added Dock Container Items and its related Panel Dock Containers
1. Red Box: Please see Red Box of Figure 6
2. Left Blue Box: Added Dock Container Item controls to the Bar control
3. Right Blue Box: Added Dock Container Item controls in the Property Grid of Visual Studio
4. Right Green Box: Added Panel Dock Container controls in the Property Grid of Visual Studio
Each time "Create Dock Tab" option is clicked under "Bar Tasks," a new Dock Container Item control with its related Panel Dock Container control is made in the Bar control of DevComponents.DotNetBar (Please see Figure 6 and 7).
Figure 8[^]
Figure 8 - Property Grid of Visual Studio after delete Bar control
- Red Box: Windows Form control in the Property Grid of Visual Studio
When Bar control is removed from Windows Form, all its related controls are removed with it, and only Windows Form remains.
I want to remove my "Tabs" property members automatically after my custom control deletes from its Parent control (Windows Form).

我找到了解决方案.若要从其父控件(Windows窗体)中删除自定义控件后自动删除"Tabs"属性成员,必须对继承自"Bar"类的"FloorsGrouping"类的"Dispose"方法进行一些更改.请参阅以下必须更改的代码行:
I found the solution. For remove "Tabs" property members automatically after deletion of custom control from its Parent control (Windows Form), must apply some changes to "Dispose" method of the "FloorsGrouping" class that inherited from "Bar" class. Please see following line of codes that must change:
protected override void Dispose(bool disposing)
{
    Tab.Clear();
    Tab.RemoveRange(0, Tab.Count);
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}


请查看为实现目标而更改的以下代码行:


Please see following line of codes that have changed for achieve to the goal:

protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }

    if (Tab.Count > 0)
    {
        foreach (FloorsInformation floorsInformation in Tab)
        {
            floorsInformation.Dispose();
        }
    }
    base.Dispose(disposing);
}