c# – 在设计模式下从表单中删除自定义控件后,清除自定义控件属性的InnerList元素
作者:互联网
我将创建的自定义控件添加到新的Windows窗体,并通过“Tabs”属性向其添加一些Tab.但是,当我从Windows窗体中删除自定义控件时,不会删除“选项卡”属性的元素.请参阅下面的数据以获取更多信息:
图1 – 我的自定义控件“Tabs”属性及其Collection Editor
> Blue Box:我的自定义控件的“Tabs”属性
>红盒:向“标签”属性添加元素
我将一些成员添加到Tabs属性时显示图1.
图2 – 将一些成员添加到Tabs属性后的Windows窗体项目控件
>红盒子:我的自定义控件
> Blue Box:向Tabs属性添加元素
图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控件.有关更多信息,请参阅以下内容:
图4 – 将Bar控件添加到Windows窗体
>左侧红色框:向Windows窗体添加了条形控件
>右红框:在Visual的属性网格中添加了条形控件
工作室
当Bar控件添加到Windows窗体时,属性网格会发生变化,如图4所示.
图5 – 通过“Bar Tasks”选项创建Dock选项卡
>红盒子:“栏任务”下的“创建码头选项卡”选项
通过“Bar Tasks”下的“Create Dock选项卡”选项,我们可以创建一个新的Dock选项卡,为Bar控件的特定选项卡添加新的控件,如图5所示.
图6 – 添加了Dock Container Item及其相关的Panel Dock Container
> Red Box:在Visual Studio的Property Grid中添加了Bar控件
>左蓝框:向Bar控件添加Dock Container Item控件
>右蓝框:在属性中添加了Dock Container Item控件
Visual Studio网格
>左侧绿色框:向Dock添加了Panel Dock Container控件
容器项控件
>右侧绿色框:在属性中添加了Panel Dock Container控件
Visual Studio网格
图7 – 添加了Dock容器项及其相关的Panel Dock容器
>红盒子:请参见图6中的红盒子
>左侧蓝框:向条形图添加了Dock Container Item控件
控制
>右蓝框:添加了Dock Container Item控件
Visual Studio的属性网格
>右绿色框:在中添加了Panel Dock Container控件
Visual Studio的属性网格
每次在“Bar Tasks”下单击“Create Dock选项卡”选项时,将在DevComponents.DotNetBar的Bar控件中创建一个新的Dock Container Item控件及其相关的Panel Dock Container控件(请参见图6和7).
图8 – 删除Bar控件后Visual Studio的属性网格
> Red Box:Visual Studio属性网格中的Windows窗体控件
从Windows窗体中删除Bar控件时,将删除其所有相关控件,并且仅保留Windows窗体.
我的自定义控件从其父控件(Windows窗体)中删除后,我想自动删除我的“选项卡”属性成员.
解决方法:
我找到了解决方案.若要从其父控件(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);
}
标签:c,winforms,custom-controls,propertygrid,design-time 来源: https://codeday.me/bug/20190620/1247301.html