我将创建的自定义控件添加到新的 Windows 窗体,并通过“选项卡”属性向其中添加一些选项卡。但是,当我从 Windows 窗体中删除我的自定义控件时,“选项卡”属性的元素不会被删除。请参阅下图了解更多信息:
图 1 - 我的自定义控件“Tabs”属性及其集合编辑器
- 蓝框:我的自定义控件的“选项卡”属性
- 红框:向“Tabs”属性添加元素
图 1 显示了当我将一些成员添加到 Tabs 属性时。
图 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 Studio 的 Property Grid 中添加 Bar 控件
当 Bar 控件添加到 Windows 窗体时,属性网格会发生变化,如图 4 所示。
图 5 - 通过“Bar Tasks”选项创建 Dock 选项卡
- 红框:“Bar Tasks”下的“Create Dock Tab”选项
使用“Bar Tasks”下的“Create Dock Tab”选项,我们可以创建一个新的 Dock Tab,以便将新控件添加到 Bar 控件的特定选项卡,如图 5 所示。
图 6 - 添加了 Dock Container Item 及其相关的 Panel Dock Container
- 红框:在 Visual Studio 的 Property Grid 中添加 Bar 控件
- 左蓝框:将 Dock Container Item 控件添加到 Bar 控件
- 右蓝框:在 Visual Studio 的 Property Grid 中添加了 Dock Container Item 控件
- 左绿框:向 Dock Container Item 控件添加了 Panel Dock Container 控件
- 右绿框:在 Visual Studio 的属性网格中添加了 Panel Dock Container 控件
图 7 - 添加的 Dock Container Items 及其相关的 Panel Dock Containers
- 红框:请看图6红框
- 左蓝框:将 Dock Container Item 控件添加到 Bar 控件
- 右蓝框:在 Visual Studio 的属性网格中添加了 Dock Container Item 控件
- 右绿框:在 Visual Studio 的属性网格中添加了 Panel Dock Container 控件
每次单击“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 窗体)中删除后,我想自动删除我的“选项卡”属性成员。