gpt4 book ai didi

c# - 在设计模式下从窗体中删除自定义控件后清除自定义控件属性的 InnerList 元素

转载 作者:太空狗 更新时间:2023-10-30 01:07:17 25 4
gpt4 key购买 nike

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

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

  • 蓝框:我的自定义控件
  • 的“Tabs”属性
  • 红框:向“Tabs”属性添加元素

  • 当我向 Tabs 属性添加一些成员时显示图 1。

    图 2 - 向 Tabs 属性添加一些成员后的 Windows 窗体项目控件
  • 红框:我的自定义控件
  • 蓝框:将元素添加到 Tabs 属性


  • 图 3 - 从 Windows 窗体中删除我的自定义控件后的 Windows 窗体项目控件
  • 红框:将元素添加到 Tabs 属性

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

    我的 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 控件。请参阅以下内容了解更多信息:

    图 4 - 将 Bar 控件添加到 Windows 窗体
  • 左红框:在 Windows 窗体中添加了 Bar 控件
  • 右红框:在Visual的属性网格中添加了Bar控件
    工作室

  • 当 Bar 控件添加到 Windows 窗体时,属性网格会发生变化,如图 4 所示。

    图 5 - 通过“Bar Tasks”选项创建 Dock Tab
  • 红框:“Bar Tasks”下的“Create Dock Tab”选项

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

    图 6 - 添加了 Dock Container Item 及其相关的 Panel Dock Container
  • 红框:在 Visual Studio 的属性网格中添加了 Bar 控件
  • 左蓝框:将 Dock Container Item 控件添加到 Bar 控件
  • 右蓝框:在属性中添加了 Dock Container Item 控件
    Visual Studio
  • 的网格
  • 左绿框:向 Dock 添加了 Panel Dock Container 控件
    容器项控制
  • 右绿框:在属性中添加了 Panel Dock Container 控件
    Visual Studio
  • 的网格


    图 7 - 添加的 Dock Container Items 及其相关的 Panel Dock Containers
  • 红框:请看图 6 的红框
  • 左蓝框:将 Dock Container Item 控件添加到 Bar
    控制
  • 右蓝框:添加了 Dock Container Item 控件中的
    Visual Studio
  • 的属性网格
  • 右绿框:在面板中添加了 Panel Dock Container 控件
    Visual Studio
  • 的属性网格

    每次单击“Bar Tasks”下的“Create Dock Tab”选项时,都会在 DevComponents.DotNetBar 的 Bar 控件中创建一个新的 Dock Container Item 控件及其相关的 Panel Dock Container 控件(请参见图 6 和图 7)。

    图 8 - 删除 Bar 控件后 Visual Studio 的属性网格
  • 红框:Visual Studio 的属性网格中的 Windows 窗体控件

  • 当 Bar 控件从 Windows 窗体中删除时,其所有相关控件也随之删除,仅保留 Windows 窗体。

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

    最佳答案

    我找到了解决方案。为了在从其父控件(Windows 窗体)中删除自定义控件后自动删除“Tabs”属性成员,必须对从“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);
    }

    关于c# - 在设计模式下从窗体中删除自定义控件后清除自定义控件属性的 InnerList 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13044221/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com