Reputation: 20464
If I have an Event like this:
' <summary>
' Event raised when conversion progress changes.
' </summary>
Public Shared Event PercentDone(ByVal percent As Integer)
Sub(...)
CoreConverter.StartInfo.FileName = CoreConverter_Location
CoreConverter.Start()
While Not CoreConverter.HasExited
If ChrW(CoreConverter.StandardOutput.Read) = "*" Then
progress += 1
RaiseEvent PercentDone(progress)
End If
End While
CoreConverter.Close()
RaiseEvent Exited()
End sub
How I can write the event handler in other class (For example in default Form1 Class) as this:
Sub Converter_Progress(Progress As Integer) Handles CoreConverter.PercentDone
' Some code...
End Sub
...Instead adding manually the handler like this else:
AddHandler CoreConverter.PercentDone, AddressOf Converter_Progress
Upvotes: 0
Views: 68
Reputation: 43743
In order to use the Handles
keyword, you need to declare the variable as a field in your class (at the class level, not local to any method) and do so using the WithEvents
keyword. For instance:
Public Class Test
Private WithEvents Converter As New CoreConverter()
Sub Converter_Progress(Progress As Integer) Handles Converter.PercentDone
' Some code...
End Sub
End Class
However, I should mention that, even for custom events, it is recommended that you follow the .NET convention for event handler delegates, where there are always two arguments: the sender and the event args. The easiest way to do that is to use the generic EventHandler(Of T)
delegate, for instance:
Public Class PercentDoneEventArgs
Inherits EventArgs
Public Property Progress As Integer
End Class
Public Class CoreConverter
Public Event PercentDone As EventHandler(Of PercentDone)
' ...
End Class
Then, your event handler method would look like this:
Sub Converter_Progress(sender As Object, e As PercentDoneEventArgs) Handles Converter.PercentDone
' ...
End Sub
Upvotes: 1
Reputation: 35260
You have to use "WithEvents" when declaring your class-level instance (in your case "CoreConverter"). For example:
Private WithEvents CoreConverter As MyConverterType
Upvotes: 1
Reputation: 565
I think you would have to inherit the base class that has the event then you can overwrite the event so then your new class gets the event, and your new class redirects the event to the base class. Or are you needing something else?
Upvotes: 1