自定义事件处理程序visualbasic

时间:2011-03-30 19:25:41

标签: .net vb.net event-handling

我的工作需要一些视觉基础编程,而且我只用C#编程。

所以我有这段代码:

Public Custom Event Command 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
        RaiseEvent()

        End RaiseEvent
    End Event

我收到错误:未对类型'ControlesModificados.ControlesModificados.JQDialogEventHandler'和'ControlesModificados.ControlesModificados.JQDialogEventHandler'定义运算符'+'。

如何以其他方式编写此代码“commandHandler - = value”,以免错误消失!或者我如何为事件处理程序定义这些运算符 谢谢。

EDITED

更多代码:

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

正如您所看到DialogCommand正在使用另一个JQDialogHandler,所以我不能只使用

Public Custom Event DialogCommand As JQDialogEventHandler

3 个答案:

答案 0 :(得分:1)

您是否需要特定的自定义实施? 您可以在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

代码更新

理想情况下,您可以这样做:     公共类JQDialogBu​​tton         继承按钮

    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

但是,由于您需要检查事件的订阅者,您可以这样做(您需要向AddHandler和RemoveHandler部分添加一些空检查):

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

像在c#中一样编写它,然后使用这个奇妙的工具将其转换为vb

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