In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve component events, delegation, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Understand component-event-delegate in C # programming
First create a Windows control project and add the following control template:
When an event is triggered, a parameter of type EventArgs is passed to the event handling method. In order to pass custom information, we can create an event parameter class that inherits from EventArgs, which is defined as follows:
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;}
Declare two more delegates, which encapsulate the information in the EventLoginArgs and EventArgs objects, as follows:
Public delegate void UserLoginEventHandler (object sender,EventLoginArgs e); public delegate void CancelEventHandler (object sender,EventArgs e)
In order to allow users to customize the handling method of an event in a component, the component must provide an event interface. If you only inherit from a single existing Windows control, you can overload the known method to add your own processing, or you can declare a custom event interface. If the component contains multiple controls, you should declare the event interface according to the actual need. Here, I declare two custom event interfaces for the use of the two buttons, as follows:
Public event UserLoginEventHandler SubmitLogin; public event CancelEventHandler Cancel; protected virtual void OnSubmitLogin (EventLoginArgs e) {if (this.SubmitLoginspell null) {SubmitLogin (this,e);}} protected virtual void OnCancel (EventArgs e) {if (this.Cancelroomnull) {Cancel (this,e);}
In fact, SubmitLogin is an example of UserLoginEventHandler delegation, what is puzzling is the trigger, delivery, and handling of this event.
In this case, the submitLogin event is triggered by determining the button:
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);}}
Note that the dialog box in this example is intended to help you understand the process of the event, and what is really useful is the second example.
In the btnOK_Click event response, a simple validity check is carried out first, and it is suggested that the actual work should be strengthened and perfected. IntLoginTime variable is the number of attempts to log in. TestUserInDB judges whether the user is legal by searching the relevant records in the database with known information. Because the component is tested through the client program, you should create the simplest client program. This is a Windows application. Add the compiled component to the user control bar, drag it to the workspace, and add the responder for the SubmitLogin event, as follows:
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);}
The following results can be obtained by running the client program at this time:
This is in test form! This is the process in DB this is the btnOK_click function!
The result shows that the OnSubmitLogin (new EventLoginArgs (txtID.Text,txtName.Text,txtPWD.Text)) in the component is executed when the btnOK button is clicked, and this method calls SubmitLogin (this,e), which triggers the SubmitLogin event, and userControl1_SubmitLogin responds, so the * * line is printed.
This is followed by the execution of TestUserInDB, which prints the second line.
* is returned to btnOK_Click to output * a row.
Component-event-delegate in C # programming: example 2
Note that if the lines of OnSubmitLogin and TestUserInDB in btnOK_Click swap positions, the results are different. In the second example, the positions of the two are exchanged, the database query is judged first, and then the result is processed in the event response userControl1_SubmitLogin of SubmitLogin. The following is the main code of example 2:
Public delegate void UserLoginEventHandler (object sender,EventLoginArgs e); public delegate void CancelEventHandler (object sender,EventArgs e); public event UserLoginEventHandler SubmitLogin; public event CancelEventHandler Cancel; protected virtual void OnSubmitLogin (EventLoginArgs e) {if (this.SubmitLogincircle null) {SubmitLogin (this,e);}} protected virtual void OnCancel (EventArgs e) {if (this. CancellogNull) Cancel (this,e) } public string Server {} public string DataBase {} public string TableSet {} public string UserForDB {} public string PWDForDB {} public bool TestUserInDB (EventLoginArgs e) {/ / MessageBox.Show ("this is the process for DB!", "TestUserInDB", MessageBoxButtons.OK); bool bOK = false; if String strConnection = "server=" + this.strServer + "; database=" + this.strDataBase + "; UID=" + this.strUserForDB + "; PWD=" + this.strPWDForDB; string strSQL = "select UserID,UserName,UserPWD from" + this.strTableSet+ "where UserID='" + e.strUserID + "'and UserName='" + e.strUserName + "' and UserPWD='" + e.strUserPWD+ "'"; SqlConnection conn = new SqlConnection (strConnection); try {conn.Open ();} catch (SqlException ex) {MessageBox.Show ("Database cannot be opened! Please check the relevant parameters. "," Error ", MessageBoxButtons.OK); return false;} SqlDataAdapter da = new SqlDataAdapter (strSQL,conn); DataSet ds = new DataSet (); try {da.Fill (ds,this.strTableSet);} catch (SqlException ex) {. } foreach (DataRow row in ds.Tables.Rows) {if (row! = null) {bOK = true;}}. } else {bOK = false;} return bOK;} private void btnOK_Click (object sender, System.EventArgs e) {if (txtID.Text! = "& txtName.Text! =" & & txtPWD.Text! = "") {intLoginTime++; bLogin = TestUserInDB (new EventLoginArgs (txtID.Text,txtName.Text,txtPWD.Text)); if (! bLogin) MessageBox.Show ("Login in Failed!", "Login Error", MessageBoxButtons.OK) Else OnSubmitLogin (new EventLoginArgs (txtID.Text,txtName.Text,txtPWD.Text));} else {MessageBox.Show ("Your must input all the items!", "Login Info", MessageBoxButtons.OK);}} private void btnCancel_Click (object sender, System.EventArgs e) {OnCancel (e);} private void UserControl_Load (object sender, System.EventArgs e) {intLoginTime = 0;}} 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;}}
Its client program is mainly as follows:
Private void userControl1_SubmitLogin (object sender, Userlogin.EventLoginArgs e) {MessageBox.Show ("This result is bLogin=" + userControl1.bLogin + "At" + userControl1.intLoginTime + "times\ nUserID=" + e.strUserID + "\ n UserName=" + e.strUserName, "TestResult", MessageBoxButtons.OK);} private void Form1_Load (object sender, System.EventArgs e) {userControl1.Server = "localhost"; userControl1.DataBase= "weiwen"; userControl1.TableSet = "TestUser"; userControl1.UserForDB= "sa" UserControl1.PWDForDB = "sa";} these are all the contents of the article "how to implement component events and delegates in C #". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.