VB.NET 2005 - "Global" event handler?

Suppose for each form in a WinForms application you want to change the cursor to WaitCursor. The obvious way to do this is to add code to any place where the form is created or displayed:

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 some form of Load event fires, show WaitCursor. When the event of the Shown form is completed, set the cursor to the default value." Thus, the Me.Cursor code can only be in one place and is not scattered throughout the application - and do not forget to put it in each instance of the form.

I assume that you can subclass the regular Form class and add cursor settings to the overridden event, but I believe that you will lose the visual designer’s ability when subclassing the Form object.

+5
source share
3 answers

To answer your question - there are not enough global events .Net to achieve what you want. There is no clean .net solution. You can take a look at Aspect-oriented programming and cross-cutting problems - there might be an AOP solution for this (some search queries will help you get started, and then send details here).

However, as it should, this is more of an idea, not a complete decision on how you can achieve this using win32 messaging.

  • , win32 Load win32, . WM_SHOWWINDOW, .
  • (.. IMessageFilter).
  • PreMessageFilter , WM_SHOWWINDOW ( ) , / reset ( Cursor.Current = Cursors.WaitCursor - )
+2

- , , mustinherit, .

+3

Another option that will not include subclassification is to add an extension method to the form type. Then you can just call your extension method (something like ShowAndWait ()) instead of show. You can even call Show if you overload it with a different signature.

+1
source

All Articles