I'm new to interfaces and I'm trying to understand how they work.
I wrote the following code, which works properly except the click event which is not firing.
Public Class Form1
Dim WithEvents Button As IClass = New MyButton
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button.Parent = Me
Public Sub ClickEventHandler(ByVal Sender As Object, ByVal E As EventArgs) Handles Button.Click
Public Interface IClass
Event Click(ByVal Sender As Object, ByVal E As EventArgs)
Public Class MyButton
Public Event click1(ByVal Sender As Object, ByVal E As System.EventArgs) Implements IClass.click
Public Property Parent1 As Object Implements IClass.Parent
Set(ByVal value As Object)
MyBase.Parent = value
You're missing a single method in the
MyButton class to make this work.
You need this:
Private Sub MyButton_Click(sender As Object, e As System.EventArgs) Handles Me.Click RaiseEvent click1(sender, e) End Sub
SimpleButton already has a click method. It's being raised when you click your derived
MyButton class. But the
Click event on
SimpleButton isn't the same event as
Click on the
IClass interface. You implemented that as
click1. So you just need to raise the
click1 method when the
Click method is raised. Hence the above method.