Link to home
Start Free TrialLog in
Avatar of MickyMc
MickyMcFlag for Ireland

asked on

Events vs Message Map

Hi there,

ok I think this is short and simple. I am looking at events and delegates in C#. So far so good, in that it seems that you create an Event in a class and add Delegates to it through a public interface that you want called when the event occurs.

So what I want to know... is the message map and WM_Mouseclick etc all gone now... is this the only way to manage events in c# windows forms.

ps.. know of any nice tutorials...

thanks M
Avatar of EdMonk
EdMonk
Flag of United Kingdom of Great Britain and Northern Ireland image

I'm not too sure exactly what you are asking. Could you provide an example of what problem you are trying to solve or can't do?

You can override the WndProc process from within a form to trap messages.

Perhaps these links will help:
http://support.microsoft.com/kb/320584
http://www.codeproject.com/KB/dotnet/devicevolumemonitor.aspx

Here is a link to the windows message constants - http://www.pinvoke.net/default.aspx/Constants/WM.html
ASKER CERTIFIED SOLUTION
Avatar of Gururaj Badam
Gururaj Badam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MickyMc

ASKER

thanks Ed,

yes I agree I asked a very bad and vague question but you are on the right track. I come from a C++ MFC background and typically deal in messages. If you wanted to notifify other controls of an event you could raise a custom message and fire it.  Controls that where interested would overide the message map and catch the messages they where interested in, otherwise pass them on.

What I want to know is in the case of custom events does the Delegates and Event mechanism replace this message map as in your example of mouse downs etc. I can see how you still use the WM messages for keys, mouse clicks etc but I'm only interested in custom messages.

So as an example of a problem. I want to fire an event in an application to let other controls know about it. I dont know if I do a custom message and let controls listen to the message handlers or use the delegates and events. As you can see Ed, I'm rusty on all this... to much time spent managing and now I'm back to good old bread and butter coding...help!!!

ps.. heres the tutorial I was following...  http://www.geekpedia.com/tutorial236_Delegates-and-Events-in-Csharp.html

rgds Mick,
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MickyMc

ASKER

thanks Ed and Novice...

great example Ed, actually one of the simplest and best I,ve come across.  Novice bang on the money with the message filters etc.

So am I correct in that I should use the Message filter etc for handling windows messages, like mouse clicks, Windows draw, etc and use the Events and delegates for custom notifications. Just trying to work out when I should use them.... rgds Mick
Not exactly.

You wish to use IMessageFilter only if you're writing an application which is interested in such message say an application like Mercury QTP. But the caveat with C# is that, using IMessageFilter can only listen to your application messages but not others say Notepad.

For events like Mouse Clicks, Windows Draw and such there's always events associated with the Window or Control. You can rely on those events, you need not take the pain of trapping raw messages.
Avatar of MickyMc

ASKER

thanks lads for all the help...

is it ok to split the points as both answers really helped.

Novice would you have a link to any guidelines like you mentioned as to when to use the different mechanisms... not to worry if you dont... thanks Mick
not exactly. I'd written my own filters while I was automating a UI for testing.

No issue with splitting.
Avatar of MickyMc

ASKER

thanks Lads... oh Ed... just noticed that you missed the null check in the example... but you did mention it alright... great example... thanks MIck