自定义控件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace EditCell { public partial class EditCell : UserControl { private string txtValue; [Description("同步Text属性的设置")]//该描述将会在属性面板中显示 [DefaultValue("Welcome")]//为属性设置初始值 public string TxtValue { get { return txtValue; } set { txtValue = value; txtEdit.Text = value; llbResult.Text = value; } } private ColorValue colorValue1; [DefaultValue(typeof(ColorValue), "Red")] public ColorValue ColorValue1 { get { return colorValue1; } set { colorValue1 = value; txtEdit.Text = value.ToString() ; llbResult.Text = value.ToString(); } } public delegate void AsyValueChangeEventHandler(object sender, AsyValueEventEvent e);//自定义事件委托 public event AsyValueChangeEventHandler AsyValueChange; private void OnAsyValueChange(string text)//当事件发生的时候,出发事件函数 { if (AsyValueChange != null)//如果在主调用中没有为AsyValueChange事件添加处理函数,不做处理 { AsyValueChange(this,new AsyValueEventEvent(text));//调用事件的处理函数 } } public EditCell() { InitializeComponent(); } private void llbResult_Click(object sender, EventArgs e) { } private void txtEdit_TextChanged(object sender, EventArgs e) { llbResult.Text = txtEdit.Text; this.txtValue=txtEdit.Text; OnAsyValueChange(this.txtValue); } } public class AsyValueEventEvent : EventArgs//自定义事件,注意用来与主调用做数据传递使用 { private string txtValue; public string TxtValue { get { return txtValue; } set { txtValue = value; } } public AsyValueEventEvent(string value) : base() { this.txtValue = value; } } public enum ColorValue { Red, Green, Blue, White, Black, Purple } }