Suppose that for every Form in a WinForms application, you want to change the cursor to the WaitCursor. The obvious way to do this would be to add the code to every place where a form is instantiated/shown:
Try Me.Cursor = Cursors.WaitCursor Dim f As New frmMyForm f.Show() Catch ex As Exception Throw Finally Me.Cursor = Cursors.Default End Try
However, I was wondering if there is a way to tell your application, ‘Whenever any form Load event fires, show a WaitCursor. When the form event Shown is complete, set the cursor back to Default.’ This way, the Me.Cursor code could be only in one place and not scattered throughout the application – and not forgotten to put it in to each form instantiation.
I suppose you could subclass the regular Form class and add the cursor settings in an overridden event, but I believe you lose the visual designer capability when you subclass the Form object.
To answer your question – there are no global .Net events to achieve what you want. There isn’y any pure .net solution to this. You could take a look at Aspect Orientated Programming and Cross Cutting Concerns – there may be an AOP solution to this (some googling will get you started then post back here for details).
However, what follows is more of an idea rather than a complete solution as to how you might achieve this using win32 messaging.