简体   繁体   中英

VB.NET equivalent of C# event hook up

This is regarding interop toolkit so UC inside another UC is a given.

I have a user control that fires up an event on button.

I've added a handler to handle it in container user control

Dim uc As New LoginUC.Login
AddHandler uc.OkClick, AddressOf InteropUserControl_UserControlOk

Now I need to raise this event in the parent user control to send it over to VB6 container form.

Public Event UserControlOk()
Private Sub InteropUserControl_UserControlOk(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.UserControlOk

    RaiseEvent UserControlOk()
End Sub

The problem that i see is that once the button is clicked this event keeps firing off non stop. In c# this would be handled by

if (this.event != null)
    this.event(sender,e);

type of code. Any advice how that can be done is VB.NET?

UPDATE:

Private Sub XamlForm_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load

    ' Create the ElementHost control for hosting the
    ' WPF UserControl.
    Dim host As New ElementHost()
    host.Dock = DockStyle.Fill

    ' Create the WPF UserControl.
    Dim uc As New LoginUC.Login
    AddHandler uc.OkClick, AddressOf InteropUserControl_UserControlOk

    ' Assign the WPF UserControl to the ElementHost
    '  control's Child property.
    host.Child = uc

    ' Add the ElementHost control to the form's
    ' collection of child controls.
    Me.Panel1.Controls.Add(host)
End Sub

This is the code I use to add UC to vb.net user control. Why would addhandler fire up multiple times?

Maybe rising the event re-triggers the UC's event again, somehow. Prevent it by adding using a guard variable

Dim raisingEvent As Boolean = False

Private Sub InteropUserControl_UserControlOk(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.UserControlOk

    If Not raisingEvent Then
        raisingEvent = True
        RaiseEvent UserControlOk()
        raisingEvent = False
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM