471,348 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Custom event handlers in .NET Winforms

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?

Thanks!
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
1 4478
On Tue, 15 Apr 2008 10:07:23 -0700, Phil Townsend <ph*******@yahoo.com
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
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by jlea | last post: by
4 posts views Thread by Paul Wilson | last post: by
12 posts views Thread by The One We Call 'Dave' | last post: by
reply views Thread by \(O\)enone | last post: by
reply views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by
2 posts views Thread by Jay Dee | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.