C # detailed description of delegation and events downloaded in PDF
Code
DOTNET standard delegation and event writing:
Public Delegate void eventhandler (Object sender, eventargs E); // defines the delegate type
Public event eventhandler click; // The Declaration event is similar to declaring an encapsulated delegate variable.
This. btnopendb. Click + = new system. eventhandler (this. btnopendb_click method); // Registration Method
Private void btnopendb_click (Object sender, eventargs e) // method to be registered
{
Conn = new sqlconnection (constring );
Conn. open ();
}
What is object sender and eventargs e?
To put it bluntly, there is a guy called eventhandler who will tell you (main program) that something has happened: who caused this? It is caused by an object type object, which is represented by source or sender. What is this? E is the content of things. As for source and sender, there is no difference. It is the same if you want to use it.
Therefore, the event processing function in the program depends on this implementation: for example, if you click a button, how does the program know which function should be used to process this action? Then the eventhandler guy will tell the program: "button1 (sender) has been clicked (e), please call the corresponding processing function ". Of course, who this function is and what it is to do is written by yourself.
Further, this process is actually: Your action is captured by windows, and Windows sends this action to the program as a system message (you can view the message structure ), the program continuously extracts messages from its own message queue and finds corresponding processing methods in the message loop, in this case, something similar to sender and E in the message structure plays the role of 'guiding program using the correct processing function.
Finally, this sender and E and their entire processing method are just another manifestation of the Windows messaging mechanism.
--------------------------------------------------------------------------------
Object Sender: the object that sends the event
System. eventargs E: Data in the object
If it is a button, the sender is the button, and E is the event parameter. In some events, e is of little use. For example, in the mouse event of mouseeventargs, we can see that E includes the coordinates of the mouse for your program.
Specifically, for system-provided class events, the sender parameter simply transmits a reference to the instance of the class that triggers the event, while E is a parameter of the eventargs type, it contains the information carried by the event.
---------------------------------------------------------------------------------
Q: "I have seen that in many event codes, all the code is written (Object sender, system. eventargs e) does not need to change the sender and E. Shouldn't the parameters change with the actual situation?"
A: the meaning of this sentence, such as a button control, when its click event is triggered, the system will automatically convert the button object as a type and assign it to sender, then pass E. In most cases, these simple events do not need to transmit special information. You can check the keypress event, which contains a keypresseventargs and key information.
---------------------------------------------------------------------------------
Another concept that needs to be clarified is a method similar to this:
Private void button_click (Object sender, eventargs E)
This method is called an event processing method. It is similar to the event processing function in C language. It is called by the system at runtime, so its parameters will not change, it is like the method name when we write the method.