Components in C # - Event - Entrusted

xiaoxiao2021-03-06  66

Transfer from: weiwenking

The understanding of the event is important in component programming, "Event" in C # is a way to provide notifications to this class when some interesting things happen. The most closely contact with the event is delegated. The delegate can be packaged in the entrusted object. In order to clarify the components - event - the relationship between the three people, I use the actual example to talk about the understanding of the younger.

First create a Windows control project, add the following control sample.

When the event is triggered, a parameter of an Eventargs type is passed to the event processing method. In order to pass custom information, we can create an event parameter class inherited in 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;

}

Two further delegate declaration, they are packaged and information EventLoginArgs EventArgs object, as follows: public delegate void UserLoginEventHandler (object sender, EventLoginArgs e); public delegate void CancelEventHandler (object sender, EventArgs e); in order to be able to assembly Let the user customize the processing method of an event, so the component must provide an event interface. If it is only inherited in a single existing Windows control, you can overload the known method to add your own processing, or declare the custom event interface. If multiple controls are included in the components, they should declare the event interface according to actual needs, and they declare two custom event interfaces on the use of two buttons, as follows: Public Event UserLogineventhandler SubmitLogin; Public Event Canceleventrandler 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 } In fact, SubmitLogin is an instance of UserLogineventHandler, which is a trigger, delivery, and processing process of this event. In this example, 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); messageb Ox.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 This example is to help understand the process, the truly useful is the second Example, in the BTNOK_CLICK event response, first check the simple validity check, it is recommended that the actual work should be enhanced. WhitLogintime variable is the number of times attempt to log in .TestUserindb is to search for the relevant record in the database in the database to determine if the user is It is legal. Because component tests pass through customer programs, you should create a simple-clear client. This is a Windows application, add compiled components to the user control bar, dragged out to workspace, add The response program of the SubmitLogin event is 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);} At this time, run the customer can be obtained: this is In Test Form! this is the process in dbthis is the btnok_click function! The result indicates that ONSubmitLogin (New EventLoginargs (TXTID.TEXT, TXTNAME.TEXT, TXTNAT, TXTNAME.TEXT, TXTPWD.TEXT) is called when clicking the BTNOK button. This method calls SubmitLogin. (this, e), so that the SubmitLogin event is thrown, and UserControl1_submitlogin responds, so it is a line of printing. Following to perform Testuserindb, it prints the second line. Finally returning to btnok_click Output Last line. Note if BTNOK_CLICK ONSUBMITLOGIN And the line modes of the TestUserindb, the result is different. In the second example, the position of the two, advance the database query judgment, and then processed the result in the SubmitLogin event response, the following is the main one. Code: 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.SubmitLogin = null!) {SubmitLogin (this, e);}} protected virtual void OnCancel (EventArgs e) {if (! this.Cancel = null) Cancel (this, e);} public string server {} public string database {} public string tableErfordb {} public string pwdfordb {}

Public Bool Testuserindb (EventLoginargs E) {

//MessageBox.show ("Testuserindb", MessageBoxButtons.ok); BOOL BOK = false; if (this.StrDatabase! = Null && this.strserver! = Null&im.struserfordb! = NULL) {if (this.strpwdfordb == null) this.strpwdfordb = ""

String straconnection = "server =" this.strserver "; database =" this.StrDatabase

"; UID =" this.strUserForDB "; PWD =" this.strPWDForDB; string strSQL = "select UserID, UserName, UserPWD from" this.strTableSet "whereUserID = '" 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." (DS, this.strtables);} catch (sqlexception ex) {...} foreach (DATAROW ROW IN DS.TABLES [this.strtableSet ].ROWS) {if (row! = null) {bok = true }} .......} Else {bok = false;} returnch 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); ElseonSubmitLogin (New EventLoginargs (TXTI D.Text, txtname.text, txtpwd.text);} else {MessageBox.show ("Your Must Input All The Items!", "Login Info", MessageBoxButtons.ok);}}

Private void btncel_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;} } as its main client: 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 two examples of complete code can click here to download. Readers can refer to learning, can also be directly With this component, but should use Microsoft SQL Server as a background database when used, and the user table used should have UserID, username, userpwd. At the same time, it should be initialized in the client program. The SubmitLogin event return value is attempts to Trial Intlogintime and Verify that the blogin is successful, can be referred to the extension example. The above is the shallow insight in learning. If you use the words or understand, please point out,

转载请注明原文地址:https://www.9cbs.com/read-87152.html

New Post(0)