自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#編程中的組件-事件-委托

開(kāi)發(fā) 后端
本文應(yīng)用實(shí)例深入分析C#編程中的組件-事件-委托三者之間的關(guān)系。

在組件編程中對(duì)事件的理解是十分重要的,C# 中的“事件”是當(dāng)對(duì)象發(fā)生某些有趣的事情時(shí),類向該類的客戶提供通知的一種方法。與事件聯(lián)系最為緊密的,個(gè)人認(rèn)為是委托.委托可以將方法引用封裝在委托對(duì)象內(nèi)。為了弄清組件-事件-委托三者的關(guān)系,本人用實(shí)際的例子來(lái)談?wù)勛约旱睦斫狻?/P>

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

首先創(chuàng)建一個(gè)Windows控件項(xiàng)目,添加如下控件樣板:

 控件樣板

當(dāng)事件觸發(fā)時(shí),會(huì)傳遞一個(gè)EventArgs類型的參數(shù)給事件處理方法,為了能傳遞自定義的信息,我們可以創(chuàng)建一個(gè)繼承于EventArgs的事件參數(shù)類,其定義如下:

  1. public class EventLoginArgs:System.EventArgs  
  2. {  
  3. public string strUserID;  
  4. public string strUserName;  
  5. public string strUserPWD;  
  6. public bool bVaild;  
  7. public EventLoginArgs(string userID,string userName,string userPWD)  
  8. {  
  9. strUserID = userID;  
  10. strUserName = userName;  
  11. strUserPWD = userPWD;  
  12.  
  13. }    
  14.  

再聲明兩個(gè)委托,它們是對(duì)EventLoginArgs和EventArgs對(duì)象中的信息的封裝,如下:

  1. public delegate void UserLoginEventHandler(object sender,EventLoginArgs e);  
  2. public delegate void CancelEventHandler(object sender,EventArgs e);  

在組件中為了能讓用戶自定義某事件的處理方法,所以組件必需提供事件接口.如果只是繼承于單個(gè)已有的Windows控件,可以重載已知的方 法進(jìn)行添加自己的處理,也可以聲明自定義的事件接口.而若組件中包含多個(gè)控件,應(yīng)該根據(jù)實(shí)際需要聲明事件接口,此處本人就兩個(gè)按鈕的 使用而聲明兩個(gè)自定義的事件接口,如下:

  1. public event UserLoginEventHandler SubmitLogin;  
  2. public event CancelEventHandler Cancel;  
  3. protected virtual void OnSubmitLogin(EventLoginArgs e)  
  4. {  
  5. if(this.SubmitLogin!=null)  
  6. {   
  7. SubmitLogin(this,e);  
  8. }  
  9.  
  10. }  
  11. protected virtual void OnCancel(EventArgs e)  
  12. {  
  13. if(this.Cancel!=null)  
  14. {  
  15. Cancel(this,e);  
  16. }   
  17.  

其實(shí)SubmitLogin 是UserLoginEventHandler委托的實(shí)例,令人費(fèi)解的是此事件的觸發(fā),傳遞,處理過(guò)程如何呢?

在本例中是通過(guò)確定按鈕來(lái)觸發(fā)submitLogin事件的:

  1. private void btnOK_Click(object sender, System.EventArgs e)  
  2. {   
  3. if(txtID.Text != ""&&txtName.Text !=""&&txtPWD.Text !="")  
  4. {  
  5. intLoginTime++;  
  6. OnSubmitLogin(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text));  
  7. bLogin = TestUserInDB(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text));  
  8. MessageBox.Show("this is the btnOK_click function!","In control",MessageBoxButtons.OK);  
  9. if(!bLogin)  
  10. MessageBox.Show("Login in Failed!""Login Error",MessageBoxButtons.OK);  
  11. }  
  12. else 
  13. {  
  14. MessageBox.Show("Your must input all the items!","Login Info",MessageBoxButtons.OK);  
  15. }  
  16. }  

注意本例中的對(duì)話框是為了幫助了解事件的過(guò)程,真正有用的是第二個(gè)例子。

在btnOK_Click事件響應(yīng)中,先對(duì)進(jìn)行簡(jiǎn)單的有效性檢查,建議實(shí)際工作應(yīng)作加強(qiáng)完善.intLoginTime變量是嘗試登錄的次數(shù).TestUserInDB是 通過(guò)已知信息在數(shù)據(jù)庫(kù)中搜索出有關(guān)記錄進(jìn)行判斷用戶是否合法. 因?yàn)榻M件的測(cè)試是通過(guò)客戶程序的,所以應(yīng)該創(chuàng)建一個(gè)最簡(jiǎn)單明了的客戶 程序.這是一個(gè)Windows應(yīng)用程序,將編譯好的組件添加到用戶控件欄中,拖出到工作區(qū)中,添加SubmitLogin事件的響應(yīng)程序,如下:

  1. private void userControl1_SubmitLogin(object sender, Userlogin.EventLoginArgs e)  
  2. {  
  3. MessageBox.Show("This is in test form!"+ userControl1.bLogin +"\ns Login times is   
  4. "+userControl1.intLoginTime +"\ne's strUserID="+e.strUserID,"Test",MessageBoxButtons.OK);  
  5. }   
  6.  

此時(shí)運(yùn)行客戶程序可得以下結(jié)果:

  1. This is in test form!  
  2. this is the process in DB  
  3. this is the btnOK_click function!   
  4.  

結(jié)果表明單擊btnOK按鈕時(shí)執(zhí)行組件中的OnSubmitLogin(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text)),此方法又調(diào)用 SubmitLogin(this,e),從而激發(fā)SubmitLogin事件,userControl1_SubmitLogin就進(jìn)行響應(yīng),故打印***行。

跟著是執(zhí)行TestUserInDB,它打印出第二行。

***是返回到btnOK_Click中輸出***一行。

C#編程中的組件-事件-委托:例子二

注意若btnOK_Click中的OnSubmitLogin和TestUserInDB所在的行調(diào)換位置,其結(jié)果是不同的.第二個(gè)例子中,二者的位置調(diào)換,先進(jìn)行數(shù)據(jù)庫(kù) 查詢判斷,再在SubmitLogin的事件響應(yīng)userControl1_SubmitLogin中處理結(jié)果,下面的是例子二的主要代碼:

  1. public delegate void UserLoginEventHandler(object sender,EventLoginArgs e);  
  2. public delegate void CancelEventHandler(object sender,EventArgs e);  
  3.  
  4. public event UserLoginEventHandler SubmitLogin;  
  5. public event CancelEventHandler Cancel;  
  6.  
  7. protected virtual void OnSubmitLogin(EventLoginArgs e)  
  8. {  
  9. if(this.SubmitLogin!=null)  
  10. {   
  11. SubmitLogin(this,e);  
  12. }  
  13.  
  14. }  
  15. protected virtual void OnCancel(EventArgs e)  
  16. {  
  17. if(this.Cancel!=null)  
  18. Cancel(this,e);  
  19. }  
  20. public string Server  
  21. {  
  22. }  
  23. public string DataBase  
  24. {  
  25. }  
  26. public string TableSet  
  27. {  
  28. }  
  29. public string UserForDB  
  30. {  
  31. }  
  32. public string PWDForDB  
  33. {  
  34. }  
  35.  
  36. public bool TestUserInDB(EventLoginArgs e)  
  37. {   
  38.  
  39.  
  40. //MessageBox.Show("this is the process for DB!","TestUserInDB",MessageBoxButtons.OK);  
  41. bool bOK = false;  
  42. if(this.strDataBase!=null && this.strServer!=null && this.strUserForDB!=null)  
  43. {   
  44. if(this.strPWDForDB==null)  
  45. this.strPWDForDB = "";  
  46.  
  47. string strConnection = "server="+this.strServer +";database="+this.strDataBase   
  48.  
  49.  
  50. +";UID="+this.strUserForDB +";PWD="+this.strPWDForDB;  
  51. string strSQL = "select UserID,UserName,UserPWD from "+this.strTableSet+" where   
  52.  
  53.  
  54. UserID='"+e.strUserID+"' and UserName='"+e.strUserName +"' and UserPWD='"+e.strUserPWD+"'";  
  55. SqlConnection conn = new SqlConnection(strConnection);  
  56. try 
  57. {  
  58. conn.Open();  
  59. }  
  60. catch(SqlException ex)  
  61. {  
  62. MessageBox.Show("數(shù)據(jù)庫(kù)不能打開(kāi)!請(qǐng)檢查有關(guān)參數(shù).","Error",MessageBoxButtons.OK);  
  63. return false;  
  64. }  
  65. SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);  
  66. DataSet ds = new DataSet();  
  67. try 
  68. {  
  69. da.Fill(ds,this.strTableSet);  
  70. }  
  71. catch(SqlException ex)  
  72. {  
  73. ......  
  74. }  
  75. foreach(DataRow row in ds.Tables[this.strTableSet].Rows)  
  76. {  
  77. if(row != null)  
  78. {  
  79. bOK = true;   
  80. }  
  81.  
  82. }  
  83. .......  
  84. }  
  85. else 
  86. {  
  87. bOK = false;  
  88. }  
  89. return bOK;  
  90. }  
  91. private void btnOK_Click(object sender, System.EventArgs e)  
  92. {   
  93. if(txtID.Text != ""&&txtName.Text !=""&&txtPWD.Text !="")  
  94. {  
  95. intLoginTime++;  
  96. bLogin = TestUserInDB(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text));  
  97. if(!bLogin)  
  98. MessageBox.Show("Login in Failed!""Login Error",MessageBoxButtons.OK);  
  99. else 
  100. OnSubmitLogin(new EventLoginArgs(txtID.Text,txtName.Text,txtPWD.Text));  
  101. }  
  102. else 
  103. {  
  104. MessageBox.Show("Your must input all the items!","Login Info",MessageBoxButtons.OK);  
  105. }  
  106. }   
  107.  
  108. private void btnCancel_Click(object sender, System.EventArgs e)  
  109. {  
  110. OnCancel(e);  
  111. }  
  112.  
  113. private void UserControl_Load(object sender, System.EventArgs e)  
  114. {  
  115. intLoginTime = 0;  
  116.  
  117. }  
  118. }   
  119.  
  120. public class EventLoginArgs:System.EventArgs  
  121. {  
  122. public string strUserID;  
  123. public string strUserName;  
  124. public string strUserPWD;  
  125. public bool bVaild;  
  126. public EventLoginArgs(string userID,string userName,string userPWD)  
  127. {  
  128. strUserID = userID;  
  129. strUserName = userName;  
  130. strUserPWD = userPWD;  
  131.  
  132. }  
  133. }  
  134.  

它的客戶程序主要如下:

  1. private void userControl1_SubmitLogin(object sender, Userlogin.EventLoginArgs e)  
  2. {  
  3.  MessageBox.Show("This result is bLogin="+ userControl1.bLogin +" At "+userControl1.intLoginTime +" times \n   
  4.   UserID="+e.strUserID+"\n UserName="+e.strUserName,"TestResult",MessageBoxButtons.OK);  
  5. }  
  6. private void Form1_Load(object sender, System.EventArgs e)  
  7. {  
  8.  userControl1.Server = "localhost";  
  9.  userControl1.DataBase="weiwen";  
  10.  userControl1.TableSet = "TestUser";  
  11.  userControl1.UserForDB="sa";  
  12.  userControl1.PWDForDB = "sa";  
  13. }   
  14.  

以上就對(duì)C#編程中的組件-事件-委托做出了一些介紹。讀者可以參考學(xué)習(xí),也可直接使用此組件,但使用時(shí)應(yīng)當(dāng)以Microsoft SQL Server 作為后臺(tái)數(shù)據(jù)庫(kù),所用到的用戶表格應(yīng)有 UserID,UserName,UserPWD三列,同時(shí)在客戶程序中應(yīng)對(duì)有關(guān)參數(shù)初始化,SubmitLogin事件返回值是嘗試次數(shù)intLoginTime和驗(yàn)證是否成功bLogin,可參考擴(kuò)展例子二。

【編輯推薦】

  1. 淺談C#中構(gòu)造函數(shù)和成員函數(shù)
  2. C#函數(shù)的參數(shù)返回結(jié)構(gòu)數(shù)組
  3. 概述ASP.NET中的NGWS Runtime
  4. C#函數(shù)與JavaScript函數(shù)
  5. 詳解C# Object.Equals函數(shù)
責(zé)任編輯:book05 來(lái)源: 網(wǎng)易博客
相關(guān)推薦

2024-06-28 10:19:02

委托事件C#

2024-05-16 13:36:04

C#委托事件

2024-05-15 09:11:51

委托事件C#

2024-06-25 08:43:25

C#編程模型

2011-06-30 10:28:50

C#開(kāi)發(fā)

2009-08-18 10:54:17

C#事件和委托

2009-08-27 16:53:01

C#委托C#事件

2009-08-26 14:27:54

C#委托和事件

2009-10-09 09:07:40

C#委托和事件

2009-08-04 13:53:58

C#委托類C#事件

2024-10-05 00:00:35

Action?C#Func?

2024-05-30 12:24:03

C#開(kāi)發(fā)

2024-12-23 08:00:00

委托C#編程

2024-09-29 09:28:38

Action?C#

2009-08-20 18:11:08

C#異步委托

2009-09-08 15:28:24

C#委托

2009-09-17 16:41:12

C#組件編程

2009-08-20 18:37:52

委托C#異步委托

2013-03-19 09:48:38

C#

2009-08-26 14:48:05

C#委托與事件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)