深入理解C#編程中的組件-事件-委托

字號:

在組件編程中對事件的理解是十分重要的,C# 中的“事件”是當(dāng)對象發(fā)生某些有趣的事情時,類向該類的客戶提供通知的一種方法。與事件聯(lián)系最為緊密的,個人認(rèn)為是委托.委托可以將方法引用封裝在委托對象內(nèi)。為了弄清組件-事件-委托三者的關(guān)系,本人用實際的例子來談 談小弟的理解。
    首先創(chuàng)建一個Windows控件項目,添加如下控件樣板。
    當(dāng)事件觸發(fā)時,會傳遞一個EventArgs類型的參數(shù)給事件處理方法,為了能傳遞自定義的信息,我們可以創(chuàng)建一個繼承于EventArgs的事件參數(shù) 類,其定義如下:
    public class EventLoginArgs:System.EventArgs
    {
    public string strUserID;
    public string strUserName;
    public string strUsERPWD;
    public bool bVaild;
    public EventLoginArgs(string userID,string userName,string userPWD)
    {
    strUserID = userID;
    strUserName = userName;
    strUserPWD = userPWD;
    }
    再聲明兩個委托,它們是對EventLoginArgs和EventArgs對象中的信息的封裝,如下:
    public delegate void UserLoginEventHandler(object sender,EventLoginArgs e);
    public delegate void CancelEventHandler(object sender,EventArgs e);
    在組件中為了能讓用戶自定義某事件的處理方法,所以組件必需提供事件接口.如果只是繼承于單個已有的Windows控件,可以重載已知的方 法進行添加自己的處理,也可以聲明自定義的事件接口.而若組件中包含多個控件,應(yīng)該根據(jù)實際需要聲明事件接口,此處本人就兩個按鈕的 使用而聲明兩個自定義的事件接口,如下:
    public event UserLoginEventHandler SubmitLogin;
    public event CancelEventHandler Cancel;
    protected virtual void OnSubmitLogin(EventLoginArgs e)
    {
    if(this.SubmitLogin!=null)
    {
    SubmitLogin(this,e);
    }
    }
    protected virtual void OnCancel(EventArgs e)
    {
    if(this.Cancel!=null)
    {
    Cancel(this,e);
    }
    其實SubmitLogin 是UserLoginEventHandler委托的實例,令人費解的是此事件的觸發(fā),傳遞,處理過程如何呢?
    在本例中是通過確定按鈕來觸發(fā)submitLogin事件的:
    private void btnOK_Click(object sender, System.EventArgs e)
    {
    if(txtID.Text != ""&&txtName.Text !=""&&txtPWD.Text !="")
    {
    intLoginTime++;
    OnSubmitLogin(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text));
    bLogin = TestUserInDB(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text));
    MessageBox.Show("this is the btnOK_click function!","In control",MessageBoxButtons.OK);
    if(!bLogin)
    MessageBox.Show("Login in Failed!","Login Error",MessageBoxButtons.OK);
    }
    else
    {
    MessageBox.Show("Your must input all the items!","Login Info",MessageBoxButtons.OK);
    }
    }
    注意本例中的對話框是為了幫助了解事件的過程,真正有用的是第二個例子。
    在btnOK_Click事件響應(yīng)中,先對進行簡單的有效性檢查,建議實際工作應(yīng)作加強完善.intLoginTime變量是嘗試登錄的次數(shù).TestUserInDB是 通過已知信息在數(shù)據(jù)庫中搜索出有關(guān)記錄進行判斷用戶是否合法. 因為組件的測試是通過客戶程序的,所以應(yīng)該創(chuàng)建一個最簡單明了的客戶 程序.這是一個Windows應(yīng)用程序,將編譯好的組件添加到用戶控件欄中,拖出到工作區(qū)中,添加SubmitLogin事件的響應(yīng)程序,如下:
    private void userControl1_SubmitLogin(object sender, Userlogin.EventLoginArgs e)
    {
    MessageBox.Show("This is in test form!"+ userControl1.bLogin +"\ns Login times is
    "+userControl1.intLoginTime +"\ne's strUserID="+e.strUserID,"Test",MessageBoxButtons.OK);
    }
    此時運行客戶程序可得以下結(jié)果:
    This is in test form!
    this is the process in DB
    this is the btnOK_click function!
    結(jié)果表明單擊btnOK按鈕時執(zhí)行組件中的OnSubmitLogin(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text)),此方法又調(diào)用 SubmitLogin(this,e),從而激發(fā)SubmitLogin事件,userControl1_SubmitLogin就進行響應(yīng),故打印第一行。
    跟著是執(zhí)行TestUserInDB,它打印出第二行。
    最后是返回到btnOK_Click中輸出最后一行。