我有一个字符串变量,或字符串属性,哪个都成。这个变量或属性在一个事件里接收值。现在我想一但这个变量或属性接收到值,我就触发一个事件通知我接收到值了。我知道用委托,但不会写。这个能不能办到?

解决方案 »

  1.   

    ms-help://MS.NETFrameworkSDKv1.1.CHS/cpguidenf/html/cpconevents.htm
      

  2.   

    不如做个隐藏的label,然后通过这个的值变挂载事件,还有我觉得如果是字符窜的话这个是值类型.
      

  3.   

    先创建事件类using System;namespace myClassLibary
    {
    /// <summary>
    /// 事件类
    /// </summary>
    ///     public delegate void ClassInEventDelegate(object sender,string strEvent);
    public class ClassInEvent
    { public event ClassInEventDelegate ClassInEvent_On; public ClassInEvent()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
            
    public void ImpEvent(string name)
      {
    if(ClassInEvent_On != null)
    {
    this.ClassInEvent_On(this,name);
    }
    else
    {
                    System.Windows.Forms.MessageBox.Show("没有绑定事件方法!");  }
             
    }
    }
    }
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace WindowsApplication1
    {
    /// <summary>
    /// myevent 的摘要说明。
    /// </summary>
    public class myevent : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    private string myname;
    private myClassLibary.ClassInEvent cie;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public string Getname
    {
    set 
    {
                    myname=value;
       
    cie.ImpEvent("lypink");
    }
                
    } private void cie_ClassInEvent_On(object sender, string strEvent)
    {
    MessageBox.Show(this,strEvent + " string 有值了");
    } public myevent()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();     cie = new myClassLibary.ClassInEvent();
    cie.ClassInEvent_On +=new myClassLibary.ClassInEventDelegate(cie_ClassInEvent_On); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(72, 48);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(128, 21);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(104, 96);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(72, 24);
    this.button1.TabIndex = 1;
    this.button1.Text = "Give str";
    this.button1.Click += new System.EventHandle.(this.button1_Click);
    // 
    // myevent
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Name = "myevent";
    this.Text = "myevent";
    this.Load += new System.EventHandler(this.myevent_Load);
    this.ResumeLayout(false); }
    #endregion private void myevent_Load(object sender, System.EventArgs e)
    {

    } private void button1_Click(object sender, System.EventArgs e)
    {
    if(textBox1.Text!="")
    {
                    this.Getname = textBox1.Text;
    }
    }
    }
    }