Custom Events in C#

Post Reply
User avatar
Click16
Posts: 1941
Joined: Mon Dec 31, 2007 4:36 am
Location: United States

Custom Events in C#

Post by Click16 »

Hey everyone! Today I will be showing you how to create and use your own events.

Have you ever wanted your own event? Something to occur when something happens? Well, Today I will be showing you how to do just that. Lets get to it!

You will need knowledge of Delegate voids, Event calling / throwing, some basic skills.

Section 1 - Ready your project.

I will be using a custom control for this, so I will need my Windows Forms Project and a class library.
In your class library, Add these references:

System.Windows.Forms
System.Drawing
System.ComponentModel
System.Diagnostics
System.Runtime

You don't need all of them, but you might want to add those references so you don't get lost somewhere.

Add your Class library as a reference for Form1. This will make it easier to access and edit our control from a different project.

Now let's inherit a control for our class library.

Code: Select all

namespace Event
{
    public class CustomControl : Control
    {
    }
}
That is My inheritance of a Control.

Create these regions:
Regions
Events
Properties
Constructor
Methods

Code: Select all

public class CustomControl : Control
    {
        #region Regions
        #endregion
        #region Events
        #endregion
        #region Properties
        #endregion
        #region Constructor
        #endregion
        #region Methods
        #endregion
    }
That should be it for Customization.

Section 2 - Declaring our Event

In the Events region, create these three regions:
Public Event Handlers
Public Events

Code: Select all

public class CustomControl : Control
    {
        Regions
        #region Events
        #region Public Event Handlers
        #endregion
        #region Public Events
        #endregion
        #endregion
        Properties
        Constructor
        Methods
    }
Lets Start with the Constructor.

Under the Constructor Region type this

Code: Select all

public <ClassName>()
        {
        }
Mine looks like so:

Code: Select all

public CustomControl()
        {
        }
Now lets create the Handler.

In the Event Handler region type this.

Code: Select all

#region Public Event Handlers
        //String being Used
        public string NewString;
        //Delegate Void
        public delegate void StringChangedHandler(string StringText);
        #endregion
That should cover your event handler. We now need to make the event. Go to the events region and add this code:

Code: Select all

#region Public Events
        //Public Event
        public event StringChangedHandler StringChanged;
        //Public String
        public string TextString
        {
            get { return this.NewString; }
            set
            {
                //Setting the String
                this.NewString = value;
                //Making sure it is not null
                if (this.StringChanged != null)
                {
                    //Setting the event
                    this.StringChanged(value);
                }
            }
        }
        #endregion
Basically, you could figure out how to replace the certain things with your own objects.

Please, download the Video Results. (I thought it would be easier to make a video.)
Results.rar
Video Results for the Program.
(1.97 MiB) Downloaded 468 times
Also, Download the Source!
CustomEvents.rar
The Source Code for this Application. Includes the Windows.FormsApplication Project, and the ClassLibrary project.
(45.33 KiB) Downloaded 409 times
Image
Post Reply