Beginner notes (C # events ),
1. Declare events
EventHandlerIs a predefined delegate that defines a method without return values. In. Net Fromwork, the format is as follows:
Public delegate voidEventHandler(Object sender, EvenArgs e)
The sender type is Object, which indicates the time publisher itself. e is used to transmit event-related information. The data type is EvenArgs and its derived class.
Standard EvenArgs does not contain any event data. Therefore, EventHandler is dedicated to event methods that represent events that do not generate data. If you want to generate data for an event, you must provide a custom event type, which is derived from EventArgs and provides all the fields or attributes required to save the time data, in this way, the time publisher can send specific data to the receiver.
With the standard EventHandler delegate, you can declare standard events that do not contain data:
Public event EvenHandler event name;
To generate an event that contains data, you must first customize the event data type. There are two types:
(1) define the delegate before defining the event.
Public class event data type: EventArgs {// encapsulation information ;}
Public delegate return value type delegate type name (Object sender, event data type e );
Public event Delegate type event name;
(2) use generic EventHandler to define events.
Public class time data type: EventArgs {// encapsulation information ;}
Public event EventHandler <time data type> event name;
2. subscribe to events
The essence of declaring an event is to define a data variable, which does not mean that the event can be triggered successfully. (1) define a method in the receiver of the event to respond to the event. (2) associate events with event methods by creating a delegate object (also known as binding events or subscribing to events ). The class that binds events and event methods is called the time subscriber.
Event name + = new event Delegate name (event method );
Note:
(1) subscribe to events by the event receiver.
(2) Each event can have multiple handlers, which are called in sequence. If a processing program encounters an exception, the program that has not yet processed will not be able to accept the event. Therefore, it is recommended that the event handler process the event quickly and avoid exceptions.
(3) to subscribe to an event, you must see a delegate object of the same type as the event, and regard the event method as the delegate object, use the "+ =" operator to add the event Method to the event of the source object.
(4) to cancel an event subscription, use "-= ".
3. trigger events
In C #, the trigger event is the same as the delegate point, but you must use the matching event parameters. Once an event is triggered, the corresponding event method is called. If the event does not have any processing program, the event is empty.
4. Conclusion:
Step 1: define related information classes;
Step 2: Define event alarms;
Step 3: declare the method called when an event is generated;
Step 4: Subscribe to events;
Step 5: trigger the event;
5. Use the event-driven model for program design. The process consists of five parts:
(1) define event-related information categories;
(2) Declare the event in the event publisher class (event source) and declare a method to trigger the event;
(3) Declare the method called when an event is generated in the event receiver class;
(4) subscribe to events in the event receiver class;
(5) trigger an event in the event receiver class.
I hope you can give me more instructions !!!
Do you have any C Language Study Notes or prepared some documents?
Hundred examples of C language programming: it is easier to learn from examples. It is better than simply reading those books with principles //
Is it a procedural test or a theoretical test?
Recommended program design guidance and online practices for trial use, outline-level books. Link: ai.pku.edu.cn/book/
The theoretical written examination must be dominated by Tan haoqiang, but it is indeed a bit messy. You may wish to buy a related exercise or something like this. Tan haoqiang has many related exercises in this book, which are basically the same, choose based on the existing materials on hand. We will not recommend it.
C language beginners, what programming software is better for laptops?
Vc6.0 this has a learning version of the Professional Edition of the Enterprise Edition
If you just want to learn, the learning version will usually be dozens of megabytes, and the enterprise edition will be several hundred megabytes