Many c # beginners are confused about events and interfaces, but do not understand the relationship between them. I will use examples to give a simple analysis and explanation.
Event. The event modifier is used to represent an event. To create a C # event, you must scan the event in the following order:
1. Create or identify a representative. For example
Public delegate void dele (); Declaration indicates that the delegate keyword notifies the compiler that dele is a delegate type.
2. Create a class that contains the methods represented by event processing and called the methods represented by event processing, as shown in the following example:
Public class EventClass1: IEvents file: // IEvents. The following is an interface
{
Public event dele event1; defines event member event1
Public void FireEvent () file: // when an event occurs
{
Event1 (); Call event processing
}
}
EventClass1 inherits the IEvents interface, which is later than EventClass2 ~ 4. They are all the same.
3. Define one or more classes that connect methods to events
4. Use Events
4.1 define the event response method, as shown in the following example:
IEvents id1 = new EventClass1 ();
4.2 Use the defined constructor to create an object containing the event, as shown in the following example:
Id1.event1 + = new dele (EventFired1 );
4.3 trigger event, as shown in the following example
Id1.FireEvent ();
Next let's take a look at the interface. We must use the interface to declare an interface. The interface declaration can declare zero or multiple members. The interface must be a method, attribute, event, or indexer. An interface cannot contain constants, fields, operators, instance constructors, destructor, or types, nor static members of any type.
All interface members are implicitly granted public access permissions. The interface member declaration contains any modifier which is a compilation error. Specifically, the interface member contains any of the following modifiers which are compile-time errors: abstract, public, protected, internal, private, virtual, override, or static. For more information, see msdn help: // MS. VSCC/MS. MSDNVS.2052/csspec/html/vclrfcsharpspec_13_1.htm.
In the following example, we declare the IEvents interface, a method FireEvent and an event event1.
Public interface IEvents
{
Event dele event1; define an event
Void FireEvent (); // defines the interface
}
In the following EventClass1 ~ Class 4 inherits the interface IEvent. Therefore, you must implement the preceding method and an event in these classes. The following examples help you better understand.
This is a simple windows Forms, including a textbox, several labels, and a button. When the program is started, the focus is on the textbox, capturing the keyboard press event, except for the direction key, I can press the event arrow key through the interface.
The following code is a common online routine. You can copy it and save it as a. cs file. compile it with CSC.
The Code is as follows:
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;
Namespace Events_Interfaces
{
Public delegate void dele (); // The Declaration indicates that the delegate keyword notifies the compiler that dele is a delegate type.
Public interface IEvents file: // defines the interface IEvents, including the method FireEvent event event1
{
Event dele event1;
Void FireEvent ();
}
Public class Form1: System. Windows. Forms. Form
{
Private System. Windows. Forms. Label label1;
Private System. Windows. Forms. TextBox textBox1;
Private System. Windows. Forms. Label label2;
Private System. Windows. Forms. Button button1;
Private System. Windows. Forms. Label label3;
Private System. ComponentModel. Container components = null;
Public Form1 ()
{
InitializeComponent ();
}
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region Windows Form Designer generated code
Private void InitializeComponent ()
{
This. textBox1 = new System. Windows. Forms. TextBox ();
This. label1 = new System. Windows. Forms. Label ();
This. button1 = new System. Windows. Forms. Button ();
This. label2 = new System. Windows. Forms. Label ();
This. label3 = new System. Windows. Forms. Label ();
This. SuspendLayout ();
This. textBox1.Location = new System. Drawing. Point (8, 80 );
This. textBox1.Name = "textBox1 ";
This. textBox1.Size = new System. Drawing. Size (56,23 );
This. textBox1.TabIndex = 1;
This. textBox1.Text = "";
This. textBox1.KeyDown + = new System. Windows. Forms. KeyEventHandler (this. Key_Press );
This. label1.Location = new System. Drawing. Point (16, 16 );
This. label1.Name = "label1 ";
This. label1.Size = new System. Drawing. Size (256, 64 );
This. label1.TabIndex = 0;
This. label1.Text = "Whenever you use the arrow keys inside the text box, Corresponding events will be" + "fired to display the label appropriately. Have a try !! ";
This. button1.Location = new System. Drawing. Point (240,112 );
This. button1.Name = "button1 ";
This. button1.Size = new System. Drawing. Size (48, 23 );
This. button1.TabIndex = 3;
This. button1.Text = "Exit ";
This. button1.Click + = new System. EventHandler (this. button#click );
//
// Label2
//
This. label2.Location = new System. Drawing. Point (88, 80 );
This. label2.Name = "label2 ";
This. label2.Size = new System. Drawing. Size (184,23 );
This. label2.TabIndex = 2;
This. label2.TextAlign = System. Drawing. ContentAlignment. MiddleCenter;
//
// Label3
//
This. label3.Location = new System. Drawing. Point (8,104 );
This. label3.Name = "label3 ";
This. label3.Size = new System. Drawing. Size (64, 23 );
This. label3.TabIndex = 4;
This. label3.TextAlign = System. Drawing. ContentAlignment. MiddleCenter;
//
// Form1
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 16 );
This. ClientSize = new System. Drawing. Size (292,141 );
This. Controls. AddRange (new System. Windows. Forms. Control [] {this. label3, this. button1, this. label2, this. textBox1, this. label1 });
This. Font = new System. Drawing. Font ("ComicSansMS", 8.25F, System. Drawing. FontStyle. Regular,
System. Drawing. GraphicsUnit. Point, (System. Byte) (0 )));
This. Name = "Form1 ";
This. Text = "Events ";
This. ResumeLayout (false );
}
# Endregion
Static void Main ()
{
Application. Run (new Form1 ());
}
Private void Key_Press (object sender,
System. Windows. Forms. KeyEventArgs e)
{
TextBox1.Text = "";
Label2.Text = "";
String keyId = e. KeyCode. ToString ();
Switch (keyId) // determines whether to press the direction key
{
Case "Right ":
Label3.Text = "";
IEvents id1 = new EventClass1 ();File: // realSample an Interface
Id1.event1 + = new dele (EventFired1); // defines the Event Response Method in EventClass1
Id1.FireEvent (); // call the FireEvent method in EventClass1 to trigger the event1 event. The event calls the EventFired1 method.
Break;
Case "Left ":
Label3.Text = "";
IEvents id2 = new EventClass2 ();
Id2.event1 + = new
Dele (EventFired2 );
Id2.FireEvent ();
Break;
Case "Down ":
Label3.Text = "";
IEvents id3 = new EventClass3 ();
Id3.event1 + = new
Dele (EventFired3 );
Id3.FireEvent ();
Break;
Case "Up ":
Label3.Text = "";
IEvents id4 = new EventClass4 ();
Id4.event1 + = new
Dele (EventFired4 );
Id4.FireEvent ();
Break;
Default:
Label3.Text = keyId;
Break;
}
}
// EventFired1 Method
Public void EventFired1 ()
{
Label2.Text = "";
Label2.Text = "You pressed RIGHT arrow key ";
}
Public void EventFired2 ()
{
Label2.Text = "";
Label2.Text = "You pressed LEFT arrow key ";
}
Public void EventFired3 ()
{
Label2.Text = "";
Label2.Text = "You pressed DOWN arrow key ";
}
Public v