Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Custom Events | Interactive GUI
Advanced C# with .NET

Custom EventsCustom Events

The following explanation is the textual form of what was taught in the video, however it is recommended to go through the textual material as well for a more thorough understanding of the topic.

In the previous chapter, we learnt how to bind methods to event handlers, however, is it possible to create our own Events? Yes, in fact it is also quite simple to do so.

An event system consists of two parts: The Publisher class and the Subscriber class. There is always a single Publisher class for an event, however there can be multiple Subscriber classes.

A Publisher class contains the event handler delegate for the event, and a method to invoke that delegate whenever relevant.

A class is referred to as a Subscriber class in this context if it subscribes any method to the event handler of the Publisher class.

The following diagram shows the process flow of an Event System:

We will create a simple event system starting from the default project. Inside the C# file, we first create a new class called CounterEvent which is going to represent the Publisher class, and it's going to have an EventHandler instance which we create using the already existing EventHandler type:

cs

index.cs

This class also needs a method to invoke the event handler whenever necessary. We can create an event which is triggered whenever the condition count % 10 == 0 is satisfied, where count is the number of times the user has clicked the CounterBtn. Let's first define the invoke method inside the CounterEvent, we will call is CountCheck because it will basically check the value of count every time it's called and it will invoke AtTensMultiple when the condition is satisfied.

The term invoke method or invoker method simple refers to a method which invokes the event handler. While event handler method refers to a method subscribed to an event handler.
cs

index.cs

Here this argument is being passed to represent the sender and new EventArgs() represents a new instance of an empty class. We can pass data into the methods that have subscribed to the event handler, when the event handler is invoked, using this second argument, however since we are passing EventArgs which is an empty class, no data is passed. We will see how to pass data this way in a bit.

Now that the invoke method is defined, our publisher class is pretty much complete. We need to do two more things:

  1. Create an instance of the publisher class;
  2. Call the invoke method to make it functional;
Calling the invoker method from the CountCheck is just one of implementing the functionality of the Event. You may employ different techniques based on the Event you are creating. For-example, running a process in the background which continously checks for the conditions of an Event to be met.

So, first I will create an instance inside the MainPage class:

cs

index.cs

And now, let's call this method inside OnCounterClicked so that it runs a check every time the user clicks the button.

cs

index.cs

Now that our event is functional, we are ready to bind a method to the event handler. I will create a new method called Reformat which simply appends (Ten's Multiple) text to the end of the button text whenever the method is invoked. Our complete code will look something like this by now:

cs

index.cs

So now if we run the program and click the button 10 times, the text (Ten's Multiple) will appear in the button text. If you press it 20 times, the same will happen again. And so on.

Now, to pass arguments into the Reformat method, we can create a new class called CustomEventArgs derived from EventArgs, and can have some public properties in that class which can represent the parameters:

cs

index.cs

Now that we have a CustomEvenArgs class, we need make a few changes to the CounterEvent class:

cs

index.cs

In the code above, we first create a new definition for the EventHandler and call it CustomEventHandler, where we set the datatype of the second parameter to CustomEventArgs.

Secondly, we pass new CounterArgs("something random") as a second argument when invoking the event handler. This lets us mass the string data (something random) as an argument to the Refactor method, which we can access like this:

cs

index.cs

So, now, our final code should look something like this:

cs

index.cs

If you run the above program, the text (something random) will be appended to the end of the button text whenever the count is a multiple of ten. Which indicates that the data is successfully being passed into the subscriber methods.

Important Points:

  • The Publisher Class contains the definition and an instance of the custom event handler delegate;
  • The can skip the definition of the Event Handler Delegate if we are using EventHandler delegate type to create the instance;
  • There is a method in the Publisher Class which is responsible for invoking the delegate whenever it's relevant;
  • The Delegate Instance should be public to make it accessible for subscriber classes;
  • We can pass arguments into Event Handler Methods by creating our own class which derives from EventArgs and having properties that represent the data or arguments which we want to pass;
1. Which keyword do we use for converting a normal delegate into an event handler?
2. Which class do we need to derive from when creating custom event method arguments?

Which keyword do we use for converting a normal delegate into an event handler?

Виберіть правильну відповідь

Which class do we need to derive from when creating custom event method arguments?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 5
course content

Зміст курсу

Advanced C# with .NET

Custom EventsCustom Events

The following explanation is the textual form of what was taught in the video, however it is recommended to go through the textual material as well for a more thorough understanding of the topic.

In the previous chapter, we learnt how to bind methods to event handlers, however, is it possible to create our own Events? Yes, in fact it is also quite simple to do so.

An event system consists of two parts: The Publisher class and the Subscriber class. There is always a single Publisher class for an event, however there can be multiple Subscriber classes.

A Publisher class contains the event handler delegate for the event, and a method to invoke that delegate whenever relevant.

A class is referred to as a Subscriber class in this context if it subscribes any method to the event handler of the Publisher class.

The following diagram shows the process flow of an Event System:

We will create a simple event system starting from the default project. Inside the C# file, we first create a new class called CounterEvent which is going to represent the Publisher class, and it's going to have an EventHandler instance which we create using the already existing EventHandler type:

cs

index.cs

This class also needs a method to invoke the event handler whenever necessary. We can create an event which is triggered whenever the condition count % 10 == 0 is satisfied, where count is the number of times the user has clicked the CounterBtn. Let's first define the invoke method inside the CounterEvent, we will call is CountCheck because it will basically check the value of count every time it's called and it will invoke AtTensMultiple when the condition is satisfied.

The term invoke method or invoker method simple refers to a method which invokes the event handler. While event handler method refers to a method subscribed to an event handler.
cs

index.cs

Here this argument is being passed to represent the sender and new EventArgs() represents a new instance of an empty class. We can pass data into the methods that have subscribed to the event handler, when the event handler is invoked, using this second argument, however since we are passing EventArgs which is an empty class, no data is passed. We will see how to pass data this way in a bit.

Now that the invoke method is defined, our publisher class is pretty much complete. We need to do two more things:

  1. Create an instance of the publisher class;
  2. Call the invoke method to make it functional;
Calling the invoker method from the CountCheck is just one of implementing the functionality of the Event. You may employ different techniques based on the Event you are creating. For-example, running a process in the background which continously checks for the conditions of an Event to be met.

So, first I will create an instance inside the MainPage class:

cs

index.cs

And now, let's call this method inside OnCounterClicked so that it runs a check every time the user clicks the button.

cs

index.cs

Now that our event is functional, we are ready to bind a method to the event handler. I will create a new method called Reformat which simply appends (Ten's Multiple) text to the end of the button text whenever the method is invoked. Our complete code will look something like this by now:

cs

index.cs

So now if we run the program and click the button 10 times, the text (Ten's Multiple) will appear in the button text. If you press it 20 times, the same will happen again. And so on.

Now, to pass arguments into the Reformat method, we can create a new class called CustomEventArgs derived from EventArgs, and can have some public properties in that class which can represent the parameters:

cs

index.cs

Now that we have a CustomEvenArgs class, we need make a few changes to the CounterEvent class:

cs

index.cs

In the code above, we first create a new definition for the EventHandler and call it CustomEventHandler, where we set the datatype of the second parameter to CustomEventArgs.

Secondly, we pass new CounterArgs("something random") as a second argument when invoking the event handler. This lets us mass the string data (something random) as an argument to the Refactor method, which we can access like this:

cs

index.cs

So, now, our final code should look something like this:

cs

index.cs

If you run the above program, the text (something random) will be appended to the end of the button text whenever the count is a multiple of ten. Which indicates that the data is successfully being passed into the subscriber methods.

Important Points:

  • The Publisher Class contains the definition and an instance of the custom event handler delegate;
  • The can skip the definition of the Event Handler Delegate if we are using EventHandler delegate type to create the instance;
  • There is a method in the Publisher Class which is responsible for invoking the delegate whenever it's relevant;
  • The Delegate Instance should be public to make it accessible for subscriber classes;
  • We can pass arguments into Event Handler Methods by creating our own class which derives from EventArgs and having properties that represent the data or arguments which we want to pass;
1. Which keyword do we use for converting a normal delegate into an event handler?
2. Which class do we need to derive from when creating custom event method arguments?

Which keyword do we use for converting a normal delegate into an event handler?

Виберіть правильну відповідь

Which class do we need to derive from when creating custom event method arguments?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 5
some-alt