Handling the event of a custom control on a form

I have a button in UserControl1

. I am using UserControl1

in Form1

. I want to handle the Click event of a Button in Form1

.

I tried to do the same through:

AddHandler userControl1.Button1.Click, AddressOf Button1_Click

      

and

Public Sub Button1_Click(ByVal sender As Object, ByVal args As EventArgs) Handles userControl1.Button1.Click

End Sub

      

but getting error.

+3


source to share


1 answer


Create your event on UserControl

:

Public Class UserControl1

    Public Event UC_Button1Click()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent UC_Button1Click()
    End Sub

End Class

      

And then use a new event:

AddHandler userControl1.UC_Button1Click, AddressOf Button1_Click

      




Or you can just define it like this on UserControl

and access it externally (not recommended):

Public WithEvents Button1 As System.Windows.Forms.Button

      

And then:

AddHandler uc.Button1.Click, AddressOf Button1_Click

      

+10


source







All Articles