Thomas Carlton
Thomas Carlton

Reputation: 5968

How do I handle events with interfaces in VB.NET?

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
    End Sub

    Public Sub ClickEventHandler(ByVal Sender As Object, ByVal E As EventArgs) Handles Button.Click
        MsgBox("Piwpiw !")
    End Sub
End Class

Public Interface IClass
    Event Click(ByVal Sender As Object, ByVal E As EventArgs)
    Property Parent
End Interface

Public Class MyButton
    Inherits SimpleButton
    Implements IClass

    Public Event click1(ByVal Sender As Object, ByVal E As System.EventArgs) Implements IClass.click

    Public Property Parent1 As Object Implements IClass.Parent
        Get
            Return MyBase.Parent
        End Get
        Set(ByVal value As Object)
            MyBase.Parent = value
        End Set
    End Property
End Class

What's wrong with that code?

(PS: This is just an example allowing me to understand how interfaces work and doesn't have any functional meaning.)

Upvotes: 3

Views: 4656

Answers (3)

Thomas Carlton
Thomas Carlton

Reputation: 5968

Here is the solution:

Replace MyButton class with:

Public Class MyButton
    Inherits SimpleButton
    Implements IClass

    Public Shadows Event click(ByVal Sender As Object, ByVal E As System.EventArgs) Implements IClass.Click

    Public Property Parent1 As Object Implements IClass.Parent
        Get
            Return MyBase.Parent
        End Get
        Set(ByVal value As Object)
            MyBase.Parent = value
        End Set
    End Property

    Public Sub ClickEventHandler(ByVal Sender As Object, ByVal E As EventArgs) Handles     MyBase.Click
        RaiseEvent click(Sender, E)
    End Sub
End Class

Upvotes: 1

Enigmativity
Enigmativity

Reputation: 117084

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

Essentially 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.

Upvotes: 3

Markus
Markus

Reputation: 22456

In your sample code, I don't see a line where the event is raised. In order to be able to handle an event in the event handler, you'd have a spot in your MyButton class that raises the event, i.e.:

Public Class MyButton
    Inherits SimpleButton
    Implements IClass

    Public Event click1(ByVal Sender As Object, ByVal E As System.EventArgs) Implements IClass.click

    Public Property Parent1 As Object Implements IClass.Parent
        Get
            Return MyBase.Parent
        End Get
        Set(ByVal value As Object)
            MyBase.Parent = value
        End Set
    End Property

    Public Sub SimulateClick()
        RaiseEvent click1(Me, New System.EventArgs())
    End Sub

End Class

You use the RaiseEvent statement to raise an event.
Above sample, of course, assumes that at some point in your code, you call SimulateClick instead of having a real mouse click (which would involve painting the button and reacting to several mouse events). Once the event is raised, your handler will be called. You could do this from your Form1_Load method:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Button.Parent = Me
    Button.SimulateClick()
End Sub

I have noticed that your MyButton class derives from a SimpleButton class. Instead of having the SimulateClick-method, you could also react to a click in the SimpleButton class and place the RaiseEvent statement there.

Upvotes: 1

Related Questions