About clicked events of button in. NET image Interface

Source: Internet
Author: User

However, the event is not limited to the image interface, but may be triggered by a logical judgment of the program. The object that triggers the event is called the event sender. The object that captures the event and responds to it is called the event receiver. However, the event Sender does not know which object or method will handle the event triggered by the event. Therefore, a media must exist between the event sender and the event receiver, it is clear that an object or a method of a type will respond to this event .. . NET uses the delegate as the media between the event sender and the event receiver. The delegate has only one signature, as long as the method signature matches the delegate signature, you can declare that you are interested in this delegate-type event, and receive and process it.
In order to pass some additional information to the event receiving object, the event sender object needs to write a class derived from System. EventArgs to encapsulate some data.
Copy codeThe Code is as follows:
Public class DrawEventArgs: EventArgs
{
Privatedouble m_Size;
Public DrawEventArgs (doublesize)
{
M_Size = size;
}
Public double Size
{
Get
{
Returnm_Size;
}
}
}

The following statement indicates that the delegate has two parameters: one parameter represents the sender of the event, and the other parameter represents the data encapsulated by the event.
Public delegate void ShapeSizeChanged (objectsender, DrawEventArgs e );
The ShapeSizeChanged instance can be bound to any method that matches the method signature.
The following is a custom event Delegate,
Public event ShapeSizeChanged SizeChanged;
The following is a code that is responsible for triggering events,
Copy codeThe Code is as follows:

Public class DrawManager
{
Public event ShapeSizeChangedSizeChanged;
Protectedvoid OnSizeChanged (DrawEventArgse)
{
ShapeSizeChangedtemp = SizeChanged;
// Whether a delegate is associated with the event
If (temp! = Null)
{
Temp (this, e );
}
}
Public void SizeChange (doublesize)
{
DrawEventArgse = new DrawEventArgs (size );
OnSizeChanged (e );
}
}

Then define the types of two listening events,
Copy codeThe Code is as follows:
Public class Square
{
PublicSquare (DrawManager drawManager)
{
// DrawManager. SizeChanged + = DrawSquare;
// Associate the event with the delegate
DrawManager. SizeChanged + = new ShapeSizeChanged (DrawSquare );
}
Public void DrawSquare (objectsender, DrawEventArgs e)
{
Console. WriteLine (string. Format ("TheSquare 'length = {0}", e. Size ));
}
Public void Detach (DrawManagerdrawManager)
{
// DrawManager. SizeChanged-= DrawSquare;
// Disassociate events from Delegation
DrawManager. SizeChanged-= new ShapeSizeChanged (DrawSquare );
}
}
Public class Rectangle
{
PublicRectangle (DrawManager drawManager)
{
DrawManager. SizeChanged + = DrawRectangle;
}
Public void DrawRectangle (objectsender, DrawEventArgs e)
{
Console. WriteLine (string. Format ("TheRectangle 'length = {0} and width = {1}.", e. Size * 2, e. Size ));
}
Public void Detach (DrawManagerdrawManager)
{
DrawManager. SizeChanged-= DrawRectangle;
}
}

Test code,
Copy codeThe Code is as follows:
Class Program
{
Static void Main (string [] args)
{
DrawManagerdrawManager = new DrawManager ();
Rectanglerect = new Rectangle (drawManager );
Squaresquare = new Square (drawManager );
// Events
DrawManager. SizeChange (5 );

// Release the listening event
Square. Detach (drawManager );
DrawManager. SizeChange (10 );
Console. ReadLine ();
}
}
/* Running result
The Rectangle 'length = 10 and width = 5.
The Square 'length = 5
The Rectangle 'length = 20 and width = 10.
*/

. The Event Mode in NET is very similar to the observer mode. in the following example, we use the observer mode to compare the above functions. First, we define two interfaces: IObserver and IObservable, as shown below:
Copy codeThe Code is as follows:
Public interface IObserver
{
VoidNotify (DrawEventArgs e );
}
Public interface IObservable
{
VoidRegister (IObserver observer );
VoidUnRegister (IObserver observer );
}

Below are the two observer classes after rewriting,
Copy codeThe Code is as follows:
Public class NewRectangle: IObserver
{
PrivateObserverManager m;
PublicNewRectangle (ObserverManager oManager)
{
M = oManager;
OManager. Register (this );
}
Public void Policy (DrawEventArgse)
{
Console. WriteLine (string. Format ("TheRectangle 'length = {0} and width = {1}.", e. Size * 2, e. Size ));
}
Public void Detach ()
{
M. UnRegister (this );
}
}
Public class NewSquare: IObserver
{
PrivateObserverManager m;

PublicNewSquare (ObserverManager oManager)
{
M = oManager;
OManager. Register (this );
}
Public void Policy (DrawEventArgse)
{
Console. WriteLine (string. Format ("TheSquare 'length = {0}.", e. Size ));
}
Public void Detach ()
{
M. UnRegister (this );
}
}

The following is the type of notification observer,
Copy codeThe Code is as follows:
Public class ObserverManager: IObservable
{
ProtectedArrayList arrList;
PublicObserverManager ()
{
ArrList = newArrayList ();
}
Public void Register (IObserverobserver)
{
ArrList. Add (observer );
}
Public void UnRegister (IObserverobserver)
{
If (arrList. Contains (observer ))
{
ArrList. Remove (observer );
}
}
Public void policyobservers (doublesize)
{
DrawEventArgse = new DrawEventArgs (size );
Foreach (IObserver observer inarrList)
{
Observer. Notify (e );
}
}
Public void SizeChanged (doublesize)
{
Yyobservers (size );
}
}

The following is the call code,
Copy codeThe Code is as follows:
Static void Main (string [] args)
{
ObserverManageroManager = new ObserverManager ();
NewRectanglerect = new NewRectangle (oManager );
NewSquaresquare = new NewSquare (oManager );
OManager. SizeChanged (5 );
Square. Detach ();
OManager. SizeChanged (10 );
Console. ReadLine ();
}

It is best to run the Code to make it easier to understand the subtle differences between the two modes.
For events, you can also explicitly use add and remove to write event accessors. The event accessors are usually generated by the compiler, so you can explicitly use the event accessors to modify the DrawManager type,
Copy codeThe Code is as follows:
Public class DrawManager
{
Privateevent ShapeSizeChangedm_SizeChanged;
Privatereadonly objectm_lock = new object ();
Public event ShapeSizeChangedSizeChanged
{
Add
{
Lock (m_lock)
{
M_SizeChanged + = value;
}
}
Remove
{
Lock (m_lock)
{
M_SizeChanged-= value;
}
}
}
Protectedvoid OnSizeChanged (DrawEventArgse)
{
ShapeSizeChangedtemp = m_SizeChanged;
// Whether a delegate is associated with the event
If (temp! = Null)
{
Temp (this, e );
}
}
Public void SizeChange (doublesize)
{
DrawEventArgse = new DrawEventArgs (size );
OnSizeChanged (e );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.