开发者

custom event handler visualbasic

开发者 https://www.devze.com 2023-02-20 16:36 出处:网络
My job is requiring some visual basic programming and i\'ve only programmed in C#. So i have this code:

My job is requiring some visual basic programming and i've only programmed in C#.

So i have this code:

Public Custom Event Command As JQDialogEventHandler
       开发者_JAVA技巧 AddHandler(ByVal value As JQDialogEventHandler)
            commandHandler += value
        End AddHandler
        RemoveHandler(ByVal value As JQDialogEventHandler)
            If commandHandler IsNot Nothing Then
                commandHandler -= value
            End If
        End RemoveHandler
        RaiseEvent()

        End RaiseEvent
    End Event

I'm getting the error: Operator '+' is not defined for types 'ControlesModificados.ControlesModificados.JQDialogEventHandler' and 'ControlesModificados.ControlesModificados.JQDialogEventHandler'.

How can i write this code "commandHandler -= value" in other way so the error goes away! or how can i defined those operators for the eventhandler.

Thank you.

EDITED

More code:

Namespace ControlesModificados
Public Class JQDialogButton
    Inherits Button
    Private commandHandler As JQDialogEventHandler

    Public Custom Event DialogCommand As JQDialogEventHandler
        AddHandler(ByVal value As JQDialogEventHandler)
            commandHandler += value
        End AddHandler
        RemoveHandler(ByVal value As JQDialogEventHandler)
            If commandHandler IsNot Nothing Then
                commandHandler -= value
            End If
        End RemoveHandler
    End Event
    ...
  Protected Overrides Sub OnClick(ByVal e As EventArgs)
        If commandHandler IsNot Nothing Then
            commandHandler(Me, New JQDialogEventArgs() With { _
              .CommandArgument = Me.CommandArgument, _
              .CommandName = Me.CommandName _
            })
        Else
            MyBase.OnClick(e)
        End If
    End Sub

As you can see the DialogCommand is using another JQDialogHandler, so i can't just use

Public Custom Event DialogCommand As JQDialogEventHandler


Simplified sample from MSDN

' this is the same as obj.Ev_Event += EventHandler
AddHandler Obj.Ev_Event, AddressOf EventHandler

Obj.CauseSomeEvent()

' this is the same as obj.Ev_Event -= EventHandler
RemoveHandler Obj.Ev_Event, AddressOf EventHandler


Do you need a specific custom implementation? You can do this in VB.net:

Public Event Command(sender as object, e as JQEventDialog)   ''//to define events
Public Event Command as JQDialogEventHandler   ''//to define events

RaiseEvent Command(me, e)   ''//to raise an event

Addhandler object.Event, addressof method     ''//to add a subscriber
Removehandler object.Event, addressof method   ''//to remove as subscriber

Update for your code

Ideally, this is how you would do it: Public Class JQDialogButton Inherits Button

    public event DialogCommand as JQDialogEventHandler

    Overrides Sub OnClick(ByVal e As EventArgs)

        RaiseEvent DialogCommand(Me, _
                                 New JQDialogEventArgs() With { _
                                    .CommandArgument = Me.CommandArgument, _
                                    .CommandName = Me.CommandName _
                                })

    End Sub
End Class

But as you are needing to check for subscribers to your event, you can do it like this (you will need to add some null checks to the AddHandler and RemoveHandler part):

Public Class JQDialogButton
    Inherits Button

    Private commandHandler As Eventhandler

    Public Custom Event DialogCommand As Eventhandler
        AddHandler(ByVal value As Eventhandler)
            commandHandler = [Delegate].Combine(commandHandler, value)
        End AddHandler
        RemoveHandler(ByVal value As Eventhandler)
            commandHandler = [Delegate].Remove(commandHandler, value)
        End RemoveHandler
        RaiseEvent()
            commandHandler.Invoke(Me,  New JQDialogEventArgs() With {.CommandArgument = Me.CommandArgument, .CommandName = Me.CommandName})
        End RaiseEvent
    End Event

    Protected Overrides Sub OnClick(ByVal e As EventArgs)
        If commandHandler IsNot Nothing Then
            RaiseEvent DialogCommand()
        Else
            MyBase.OnClick(e)
        End If
    End Sub
End Class


Write it as you would in c# and then use this wonderful tool to convert it to vb

http://www.developerfusion.com/tools/convert/csharp-to-vb/

0

精彩评论

暂无评论...
验证码 换一张
取 消