C # event and interface programming examples

Source: Internet
Author: User
Tags id3

Many beginners C # friends for events and interfaces are puzzled, do not understand the relationship between them, I will use examples to explain the simple analysis.

Event, which represents an event with the event modifier, we want to create a C # event that must sweep the line in the following order:

1, create or identify a delegate. For example, the public delegate void Dele () in the following example; The delegate keyword notifies the compiler that DELE is a delegate type

2, create a class that contains an event-handling delegate that invokes the method represented by the event handler, as in the following example

public class Eventclass1:ievents//ievents, is the following we want to talk about an interface
{
Public event Dele event1;//defining event members Event1
public void FireEvent ()//When an event occurs
{
Event1 (); Invoke Event handling
}
}

EventClass1 inherits the interface Ievents, the following eventclass2~4, are the same.

3, define one or more classes to connect the method to the event

4, using Events

4.1 Define the event response method, as in the following example

Ievents id1 = new EventClass1 ();

4.2 Use the defined constructor to create an object that contains the event, as in the following example

Id1.event1 + = new dele (EventFired1);

4.3 trigger event, as in the following example

Id1. FireEvent ();

Let's look at the interface, we have to use interface to declare an interface. An interface declaration can declare 0 or more members. The member of an interface must be a method, property, event, or indexer. Interfaces cannot contain constants, fields, operators, instance constructors, destructors, or types, nor can they contain static members of any kind.

All interface members implicitly have public access rights. An interface member declaration contains any modifier that is a compile-time error. Specifically, an interface member contains any of the following modifiers that 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; Defining events
void FireEvent ();//define Interface
}

The following Eventclass1~4 class inherits the interface IEvent, so one of the above methods and one event must be implemented in these classes. The following examples can help you understand better.

This is a simple Windows Forms, contains a textbox, several labels and a button, in the program startup focus in the TextBox, catch the keyboard press event, in addition to the arrow keys, I can cross the interface to touch the key press event.

The following code is a common online routine, you can copy down, save as a. cs file, with the CSC compiled on the line

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 ();//Declaration on behalf of the delegate keyword notifies the compiler that DELE is a delegate type
public interface ievents//define interface Ievents, including 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 would 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.button1_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 ("Comic sansms", 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)//Determine if the arrow keys are pressed
{
Case ' right ':
Label3. Text = "";
Ievents id1 = new EventClass1 (); Instantiate an interface
Id1.event1 + = new dele (EventFired1);//Define 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 void EventFired4 ()
{
Label2. Text = "";
Label2. Text = "You pressed up ARROW key";
}

private void Button1_Click (object sender,
System.EventArgs e)
{
Application.exit ();
}
}
public class Eventclass1:ievents
{
public event Dele Event1;
public void FireEvent ()
{
Event1 ();
}
}
public class Eventclass2:ievents
{
public event Dele Event1;
public void FireEvent ()
{
Event1 ();
}
}
public class Eventclass3:ievents
{
public event Dele Event1;
public void FireEvent ()
{
Event1 ();
}
}
public class Eventclass4:ievents//eventclass1 inherit interface ievents
{
Public event Dele event1;//defining event members Event1
When an event occurs
public void FireEvent ()
{
Event1 ();//Invoke Event handling
}
}

}

C # event and interface programming examples

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.