Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is possible via a number of possible routes. I'd prefer dependency injection in this case.</p> <p>First, create your global event owner project. I named mine GlobalEventSample. I removed the default namespace and declared it here explicitly to make the code structure more obvious:</p> <pre><code>Namespace GlobalEventSample Public Module Module1 Public Event GlobalEvent As EventHandler Public Sub Main() Console.WriteLine("Press any key to raise event...") Console.ReadKey(True) RaiseEvent GlobalEvent(Nothing, EventArgs.Empty) Console.WriteLine("Press any key to quit...") Console.ReadKey(True) End Sub End Module End Namespace </code></pre> <p>Now create the consumer project. I named mine GlobalEventConsumer. I removed the default namespace and declared it here explicitly (just as above):</p> <pre><code>Namespace GlobalEventConsumer Public Interface IGlobalEventOwner Event GlobalEvent As EventHandler End Interface Public Class Class1 Public Sub New(ByVal globalEvent As IGlobalEventOwner) AddHandler globalEvent.GlobalEvent, AddressOf GlobalEventHandler End Sub Public Shared Sub GlobalEventHandler(ByVal sender As Object, ByVal e As EventArgs) Console.WriteLine("Event Handled!") End Sub End Class End Namespace </code></pre> <p>Notice that I've declared an interface named "IGlobalEventOwner". All it does is define an object with an event. This event has a signature identical to the global event we want to handle.</p> <p>Go back to the sample project and create a reference to the consumer project.</p> <p>The consumer project requires an object which implements IGlobalEventOwner. Modules cannot implement interfaces, so we instead create a private class, GlobalEventRouter, which will simply handle the module's event and then fire its own event. Finally, we will create a new instance of Class 1 in the Main sub and pass an instance of the GlobalEventRouter class.</p> <pre><code>Namespace GlobalEventSample Public Module Module1 Public Event GlobalEvent As EventHandler Public Sub Main() Dim consumer As New GlobalEventConsumer.Class1(New GlobalEventRouter()) Console.WriteLine("Press any key to raise event...") Console.ReadKey(True) RaiseEvent GlobalEvent(Nothing, EventArgs.Empty) Console.WriteLine("Press any key to quit...") Console.ReadKey(True) End Sub Private Class GlobalEventRouter Implements GlobalEventConsumer.IGlobalEventOwner Public Event GlobalEvent(ByVal sender As Object, ByVal e As System.EventArgs) Implements GlobalEventConsumer.IGlobalEventOwner.GlobalEvent Public Sub New() AddHandler Module1.GlobalEvent, AddressOf GlobalEventHandler End Sub Private Sub GlobalEventHandler(ByVal sender As Object, ByVal e As EventArgs) RaiseEvent GlobalEvent(sender, e) End Sub End Class End Module End Namespace </code></pre> <p>The output:</p> <blockquote> <p>Press any key to raise event...<br> Event Handled!<br> Press any key to quit... </p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload