C# 利用委托事件進行窗體間的傳值(簡化)


定義委托

 public delegate void SendMessageToChildForms(string s); //定義了一個參數是string ,無返回值的委托,名為 SendMessageToChildForms。

委托實例化

// 本質就是實例化了一個事件event
 public event SendMessageToChildForms smtcf_event;

定義具體執行的方法

public void ToShowGetMessage(string s)
{
     this.lb_收到內容.Text=s;
}

綁定方法

 Parameter frm_child = new Parameter();
            smtcf_event += frm_child.ToShowGetMessage; //在一實例化的一個委托事件上綁定子窗體的具體方法
            frm_child.Show();

觸發委托

        if (smtcf_event != null) //判斷委托事件是否為空,如果委托不為空才執行
        {
            smtcf_event.Invoke("12212");// 可以省略Invoke 簡寫為smtcf(this.textBox1.Text.Trim());
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM