473,392 Members | 1,363 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,392 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 4585
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.