110,578
社区成员
发帖
与我相关
我的任务
分享
//例如,我创建了一个对象
MyClass myc1;
//定义一个控件,用来测试
/// <summary>
/// ItemCollection 的摘要说明。
/// </summary>
public class UCItemCollection : System.Windows.Forms.UserControl
{
private ItemCollections m_items;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public UCItemCollection()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
this.m_items = new ItemCollections();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
[Editor(typeof(System.ComponentModel.Design.CollectionEditor),
typeof(System.Drawing.Design.UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]//设置设计器是序列化它的内容。
public ItemCollections Items
{
get
{
return this.m_items;
}
}
}
//Item和Item的集合类
/// <summary>
/// Item
/// </summary>
public class MyItem:System.ComponentModel.Component
{
private string s_name;
private string s_value;
public MyItem()
{
//
// TODO: 在此处添加构造函数逻辑
//
this.s_name = string.Empty;
this.s_value = string.Empty;
}
/// <summary>
/// 名字
/// </summary>
public string Name
{
get
{
return this.s_name;
}
set
{
this.s_name = value;
}
}
/// <summary>
/// 数值
/// </summary>
public string Value
{
get
{
return this.s_value;
}
set
{
this.s_name = value;
}
}
}
/// <summary>
/// ItemCollection
/// </summary>
public class ItemCollections:System.Collections.CollectionBase
{
public ItemCollections()
{
}
//这个方法必须有
public MyItem this [int index]
{
get
{
return (MyItem)List[index];
}
set
{
List[index] = value;
}
}
public void Add(MyItem item)
{
this.List.Add(item);
}
public void Remove(MyItem item)
{
this.List.Remove(item);
}
public int IndexOf(MyItem item)
{
return this.List.IndexOf(item);
}
protected override void OnInsert(int index, object value)
{
if(!(value is MyItem))
{
throw new ArgumentException("插入Item不正确","未执行");
}
}
protected override void OnSet(int index, object oldValue, object newValue)
{
if(!(newValue is MyItem))
{
throw new ArgumentException("插入Item不正确","未执行");
}
}
}
class My : Component
{
private MyItem[] items;
public MyItem[] Items { get { return items; } set { items = value; } }
}
[ToolboxItem(false)]
[DesignTimeVisible(false)]
class MyItem : Component
{
//public int Value { get; set; }
}
class My : Component
{
private string[] items;
public string[] Items
{
get { return items; }
set { items = value; }
}
}