Re: Custom event handlers in .NET Winforms

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Tue, 15 Apr 2008 10:07:23 -0700, Phil Townsend <phil25840@xxxxxxxxx> wrote:

I have an application that needs to respond to events that occur outside
of the application itself. My project, called "ShowDetection" declares
the event. I have a console app called "TestEvent" that I would like to
use to test the event handler. Any action in the console app would be
acceptable, such as a keystroke. I am at a loss on how to go about
detecting and raising this event in my Winform. cn anybody help?

Detecting: you need some code that actually reads keyboard input. That's the code that does the detecting.

Raising: at some point after you've detected the event, you need to invoke the delegate field that represents the event. The most common pattern for doing this would be to call a method that looks like this:

void OnMyEvent()
{
MyEventHandler handler = MyEvent;

if (handler != null)
{
handler(this, new MyEventArgs());
}
}

where "MyEvent" is the name of the event that is declared using the standard .NET signature (i.e. taking two parameters, the first an object representing the sender, and the second a class containing the event data). In that pattern, "MyEventHandler" is the delegate type used for the event and "MyEventArgs" is the class containing the event data (of course, usually you'd pass that data to the constructor or otherwise initialize the class before invoking the event handlers).

The important thing to keep in mind is that in this context, "event" doesn't refer to some special signaling mechanism supported by the framework (a connotation that the term would have in other contexts). It's really just a built-in way of representing a collection of delegates that are called by a class at a specific point in time. Other than managing the collection of delegates, everything else needs to be implemented by the class defining the event, and the invocation of the handlers is little more than a short-hand for enumerating all of the handler delegates that were added (subscribed) to the event, calling each one at a time.

Beyond that, I recommend you read about events in the MSDN documentation, and if and when you have more specific questions about how to use them, please feel free to post those there and we can try to answer them.

Pete
.



Relevant Pages

  • Re: Help me with threads.
    ... I'll be the first to admit that it took me a while to get my head around it ... and if the IDE could write the EventArgs class ... > delegates and events work especially concerning when the auto generated ... and in the event handler do a little bit of work to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: passing an event as a parameter
    ... Events are essentially delegates that are type members ... > the base class that would take a reference to an event as a parameter ... > base class to actually connect the event to its handler. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: PaintEventHandler
    ... all the delegates attached to the handler at the time it was copied to the local "handler" variable will be executed. ... Depending on your code, you could do a search for code that adds itself to the Paint event for the class, but the only truly reliable way to know what would be executed is to break at that point in code and see what delegates have been attached to the event. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Menus from a database table and addhandler
    ... Put the procedure name in the menu item's Tag property (this a general purpose Object property that is available for us to use for anything). ... Wire up all the dynamically-created menu items to the same handler. ... Or, if you mean 'procedure' as in VB.NET procedure, you could even create appropriate delegates when you create the menu items, and put the delegates in the Tag, to be called on click. ...
    (microsoft.public.dotnet.languages.vb)