0

Say I have an member (e.g., Button1) of an object (e.g., Form1) that is delared using withevents (e.g., Form1.Button1_Click), and there is a handler with 'Handles' in that object.

If I override it (say, Form2.Button1_Click), will the handler call the overriden version (like me.Button1_Click) or the one with the actual handles on it (like MyClass.Button1_Click)?

Here's what I tried:

Public Class Form1

    Public Overridable Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("Form1's Button")
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim f2 As New Form2
        f2.Show()
    End Sub
End Class

Public Class Form2
    Inherits Form1

    Public Overrides Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MsgBox("Form2's Button")
    End Sub

End Class
Unihedron
  • 10,902
  • 13
  • 62
  • 72
FastAl
  • 6,194
  • 2
  • 36
  • 60

2 Answers2

0

The overridden version is called. When I click button1 on Form1, I get 'Form1's Button'. When I start the second form using button2, I click button1, and get 'form2's button'

Just so anybody tries to google this and finds nothing like I did, I killed 10 minutes of my life to test it, and now nobody else will need to!

FastAl
  • 6,194
  • 2
  • 36
  • 60
0

Specifying the Overridable modifier indicates that the method can be overridden.

To override is to reject or cancel (a decision, view, etc.).

Overrides specifies that the method will override the existing event handlers implementation. The existing method in Form1 will never be called unless you manually call it. You can manually call it by using MyBase keyword which essentially allows you to reference the base class of the current instance.

Public Overrides Sub Button1_Click(sender As Object, e As EventArgs)
    MessageBox.Show("SecondForm's Button")
    MyBase.Button1_Click(sender, e)
End Sub
User 12345678
  • 7,714
  • 2
  • 28
  • 46