Override Events in VB.NET
-
22-09-2019 - |
Question
I would like to shadow/override an event in VB.NET.
I do it only because I have to implement in this object an interface, that contains the same event like the base object itself, and I need to keep this base event as is it without modification nor supplementary event additions.
How can I do it?
Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged
So, I would like to implement a interface that contains VisibleChanged event, but to keep functional the myBase VisibleChanged event too.
Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged
AddHandler(ByVal value As EventHandler)
AddHandler MyBase.VisibleChanged, value
End AddHandler
RemoveHandler(ByVal value As EventHandler)
RemoveHandler MyBase.VisibleChanged, value
End RemoveHandler
End Event
something like this, but it seems Visual Studio does not recognize such a syntax...
I mean, in C# I realize it like this:
public new event EventHandler VisibleChanged
{
add { base.VisibleChanged += value; }
remove { base.VisibleChanged -= value; }
}
Solution
Option Strict On
Option Explicit On
Public Class Form1
Public Sub New()
' This call is required by the Windows Form Designer.'
InitializeComponent()
' Add any initialization after the InitializeComponent() call.'
Dim b As New MyButton
Me.Controls.Add(b)
b.Name = "customButton"
b.Location = New Point(10, 10)
b.Text = "Hit me!"
AddHandler CType(b, IMyInterface).Click, AddressOf MyButton1_Click
End Sub
Private Sub MyButton1_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.Print("{0} clicked!; ", CType(sender, Control).Name)
End Sub
End Class
' ------- interface'
Public Interface IMyInterface
Event Click As EventHandler
End Interface
' ------- class'
Public Class MyButton
Inherits System.Windows.Forms.Button
Implements IMyInterface
' ============ HERE IS THE SOLUTION'
Private Event Click1 As EventHandler Implements IMyInterface.Click
Private Sub ResendClick( _
ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Click
RaiseEvent Click1(sender, e)
End Sub
' END HERE IS THE SOLUTION ============ '
End Class
OTHER TIPS
When implementing an interface in VB.NET, you don't need to name the implementation the same name as the interface.
Example interface:
Public Interface IFoo
Event Bar As EventHandler
End Interface
Example implementation:
Public Class FooImplementation
Implements IFoo
' This is the built-in Bar event, specific to this class.
Public Event Bar As EventHandler
' This is the implemented IFoo.Bar event.
Public Event IFooBar As EventHandler Implements IFoo.Bar
End Class
When somebody is attached to you as an IFoo, they will get your IFooBar event:
Dim instance As IFoo = New FooImplementation()
AddHandler instance.Bar, AddressOf IFooBarHandler
When they're attached to you as FooImplementation, they'll get the Bar event and they will also be able to attach to IFooBar:
Dim instance As New FooImplementation()
AddHandler instance.Bar, AddressOf BarHandler
AddHandler instance.IFooBar, AddressOf IFooBarHandler
Can you override the handler in a derived class?
Public Class OverriddenClass
Inherits OriginalClass
Protected Overrides Function ProcessVisibleChanged(ByRef msg as Message, value as ...) As Boolean
value = value + 1
ProcessVisibleChanged = MyBase.ProcessVisibleChanged(msg, value)
' or use OnVisibleChanged to avoid the base handler
End Function
end class
Public Shadows Event VisibleChanged As EventHandler Implements IVisibleChanged
AddHandler(ByVal value As EventHandler)
AddHandler MyBase.VisibleChanged, value
End AddHandler
RemoveHandler(ByVal value As EventHandler)
RemoveHandler MyBase.VisibleChanged, value
End RemoveHandler
End Event
something like this, but it seems Visual Studio does not recognize such a syntax...
I mean, in C# I realize it like this:
public new event EventHandler VisibleChanged
{
add { base.Click += value; }
remove { base.Click -= value; }
}