473,320 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 4582
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jlea | last post by:
I've created a custom control based on TreeView and it handles several events, such as Mousedown. I added this custom control to the toolbox in another project, dragged the custom control to the...
1
by: Adam | last post by:
In Winforms, in Visual Studio, how does one add multiple event handlers in the properties box for let's say, a textbox TextChanged event? Thanks in advance. Adam
1
by: Lamont Adams | last post by:
Hi all, I've created numerous custom controls of varying complexity, but I've been on this problem for a day and a half, and I can't figure this mystery out. I hope one of you kind folks can...
4
by: Paul Wilson | last post by:
I want to use Err.Raise() method to raise my own exceptions. Is this the right way of raising my own exceptions ? (i think this is the only way). What is the Error number i can safely use,...
12
by: The One We Call 'Dave' | last post by:
Hi, Various custom controls in my WinForms application register for the 'Application.Idle' event on load, and unregister on dispose. To avoid memory leaks, it's essential that I remember to...
4
by: parez | last post by:
Hi, Whats the winforms equivalent of asp.net page load event? I am trying to clear a status message field every time a user clicks on any of the buttons..
0
by: \(O\)enone | last post by:
I'm working on some code which dynamically adds WinForms controls to a form. It's all working well but I'm having to manually call AddHandler repeatedly for each event I am using each time I add...
0
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Actually, my installer package is not for a Windows Service, but for a WinForms application. Well, it is kind of both: this is a multi-project solution with its main target being a WinForms...
2
by: Jay Dee | last post by:
I have created a container that will position 4 panels that has 15 different layouts to choose from. I have based it similar to a System.Windows.Forms.SplitContainer but that has 4 panels...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.