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

时间:2012-10-24 06:53:39

标签: c# winforms custom-controls propertygrid design-time

我将创建的自定义控件添加到新的Windows窗体,并通过“Tabs”属性向其添加一些Tab。但是,当我从Windows窗体中删除自定义控件时,不会删除“选项卡”属性的元素。有关更多信息,请参阅下面的数据:

My custom control "Tabs" property with its Collection Editor

图1 - 我的自定义控件“Tabs”属性及其集合编辑器

  1. Blue Box:我的自定义控件的“Tabs”属性
  2. 红盒子:在“标签”属性中添加了元素
  3. 图1显示了我在Tabs属性中添加一些成员的时间。

    Windows Form project controls after add some member to the Tabs property

    图2 - 将一些成员添加到Tabs属性后的Windows窗体项目控件

    1. 红盒子:我的自定义控件
    2. 蓝框:向标签属性添加元素
    3. Windows Form project controls after delete my custom control from Windows Form

      图3 - 从Windows窗体中删除我的自定义控件后的Windows窗体项目控件

      • 红盒子:向选项卡属性添加元素

      如图2和图3所示,我在Tabs属性中添加了一些成员,并在从其父控件(Windows窗体)中删除我的自定义控件后,尚未删除Tabs属性成员。

      My Tabs属性与从CollectionBase类派生的Tabs类相关,并实现了一些方法,如Add,Remove,Clear等。我在Dispose方法中调用了Clear和RemoveRange方法,但它不起作用。

      我的代码如下:

      [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控件。有关更多信息,请参阅以下内容:

      Add Bar control to the Windows Form

      图4 - 将Bar控件添加到Windows窗体

      1. 左侧红色框:向Windows窗体添加了条形控件
      2. 右红色框:在Visual的Property Grid中添加了Bar控件 工作室
      3. 当Bar控件添加到Windows窗体时,属性网格会发生变化,如图4所示。

        Create Dock Tab by "Bar Tasks" option

        图5 - 按“条形图任务”选项创建停靠栏选项卡

        • 红盒子:“栏任务”下的“创建码头选项卡”选项

        使用“Bar Tasks”下的“Create Dock选项卡”选项,我们可以创建一个新的Dock选项卡,为Bar控件的特定选项卡添加新的控件,如图5所示。

        Added Dock Container Item and its related Panel Dock Container

        图6 - 添加了Dock Container Item及其相关的Panel Dock容器

        1. 红框:在Visual Studio的属性网格中添加了条形控件
        2. 左蓝框:向条形控件添加了Dock Container Item控件
        3. 右蓝框:在属性中添加了Dock Container Item控件 Visual Studio网格
        4. 左侧绿色框:向Dock添加了Panel Dock Container控件 容器项目控制
        5. 右侧绿色框:在属性中添加了Panel Dock Container控件 Visual Studio网格
        6. Added Dock Container Items and its related Panel Dock Containers

          图7 - 添加了Dock容器项及其相关的Panel Dock容器

          1. 红盒子:请参见图6的红盒子
          2. 左侧蓝框:向条形图添加了Dock Container Item控件 控制
          3. 右蓝框:添加了Dock Container Item控件 Visual Studio的属性网格
          4. 右绿色框:在中添加了Panel Dock Container控件 Visual Studio的属性网格
          5. 每次在“Bar Tasks”下单击“Create Dock选项卡”选项时,将在DevComponents.DotNetBar的Bar控件中创建一个新的Dock Container Item控件及其相关的Panel Dock Container控件(请参见图6和7)

            Property Grid of Visual Studio after delete Bar control

            图8 - 删除条控件后Visual Studio的属性网格

            • Red Box:Visual Studio属性网格中的Windows窗体控件

            从Windows窗体中删除Bar控件后,将删除其所有相关控件,并且只保留Windows窗体。

            我想在自定义控件从其父控件(Windows窗体)中删除后自动删除我的“Tabs”属性成员。

3 个答案:

答案 0 :(得分:3)

我找到了解决方案。若要从其父控件(Windows窗体)中删除自定义控件后自动删除“选项卡”属性成员,必须对从“Bar”类继承的“FloorsGrouping”类的“Dispose”方法应用一些更改。请参阅以下必须更改的代码行:

protected override void Dispose(bool disposing)
{
    Tab.Clear();
    Tab.RemoveRange(0, Tab.Count);
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

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

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);
}

答案 1 :(得分:1)

您可以从生成的代码中删除它们,这是Form1.generated.cs文件,其中定义了InitializeComponent()。

基本上Tabs是可重复使用的,所以这可能是VS没有自动删除它们的原因,但这不过是一个猜测。

答案 2 :(得分:1)

好的,我会在这里猜测,但我从未使用过DotNetBar,所以我可能会离开。在我看来,必须将所有控件添加到Container对象以便以可视方式显示(如窗体,面板等)。如果要将这些选项卡页面添加到从Bar派生的此控件中,并且Bar不是Container,那么我必须假设在某种程度上,您的选项卡页面正被添加到表单的Controls数组中。

因此,当您删除Bar控件时,标签不会被删除,因为它们仍在范围内(因为表单仍处于活动状态)。

在澄清后编辑1

这看起来很有趣:

[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; }
}

如果您将ContainerControl设置为自身(它表示应将其设置为表单),这看起来可能是不正确的。但同样,我不确定这是否相关。