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());
        }
相關文章
相關標籤/搜索