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 tight contact with the event is the delegation. The commission can reference the method to the entrusted object. In order to clarify the components - event - entrust the relationship between the three, I use the actual example to talk about the little brother's understanding.
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;
}
Then declare two delegates, they are packaged in EventLoginargs and EventArgs objects, as follows:
Public Delegate Void UserLogineventHandler (Object Sender, EventLoginargs E);
Public Delegate Void CanceleventHandler (Object Sender, Eventargs E);
In the component, in order to allow the user to customize the processing method of an event, the component must provide an event interface. If it is only inherited in a single existing Windows control, you can overload the known approach to add your own processing, or you can declare The defined event interface. If you contain multiple controls in the component, you should declare the event interface according to the actual needs, and you will declare two custom event interfaces for the use of two buttons, as follows:
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);
}
In fact, SubmitLogin is an instance of the UserLogineventHandler, which is the trigger, delivery, and processing process 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 The dialog box in this example is to help understand the process of the event, is really useful for 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 attempts to log in .TestUserindb is to search the relevant records in the database in the database to determine whether the user is legal. Because component tests are through the customer program, you should create the simplest and clear client. This is a Windows application that adds a compiled component to the user control bar, dragging out to the workspace, add a SubmitLogin event The response program 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 point, you can run the customer program:
This is in test form!
this is the process in db
This is the btnok_click function!
The results indicate that the ONSubmitLogin (New EventLoginargs (TXTNAME.TEXT, TXTNT, TXTNAME.TEXT, TXTNTNT, TXTNAME.TEXT, TXTPWD.TEXT) is called when clicking the BTNOK button, which calls SubmitLogin (E), thus inspiring the SubmitLogin event, UserControl1_submitlogin responds Therefore, it is a line of printing.
Following to perform Testuserindb, it prints the second line.
Finally, returning to the last line of output in btnok_click.
Note that the result is different. The result is different. In the second example, the position of the two, advance the database query judgment, and then in the SubmitLogin event response UserControl1_submitlogin Procedure, below Is the main code of the second: 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 Tableset
{
}
Public String Userfordb
{
}
Public String Pwdfordb
{
}
Public Bool Testuserindb (EventLoginargs E)
{
//MessageBox.show ("Testuserindb", MessageBoxButtons.ok);
BOOL BOK = false;
IF (this.StrDatabase! = null && this.strserver! = null && this.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 "Where
Userid = '" E.STRUSERID "' AND username = '" E.STRUSERNAME "' and userpwd = '" E.STRUSERPWD "' "
SqlConnection Conn = New SqlConnection; TRY
{
Cn.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.strtables);
}
Catch (SQLException EX)
{
......
}
Foreach (Datarow Row In Ds.Tables [this.strtables] .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 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;
}
}
Its customer program is as follows: private void userControl1_submitlogin (Object Sender, UserLogin.EventLoginargs E)
{
Messagebox.show ("this result is blogin =" userControl1.blogin "at" userControl1.intlogintime "TIMES / N
Userid = " 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";
}
The full code of these two examples can click here to download.
Readers can use this component directly, but use Microsoft SQL Server as a background database when used, and the user table used should have UserID, username, and UserPwd. At the same time, relevant parameters initialization, SubmitLogin Event return value is an attempt time Intlogintime and verify that it is successful blogin, and can be referred to the extended example.