How can I RaiseEvent in VB.net

喜欢而已 提交于 2019-12-12 02:18:48

问题


I have 2 project in my Solution.
Lets say Proj A and Proj B.

Proj A is having my custom event. and same Proj is Raising that event using RaiseEvent Function of Vb.net And Proj B is having reference of Proj A.
Proj B is adding handler for Proj A's custom event.

but my custom event cant raise. Could any one can explain me how can I do that.?

Edit:

Proj A

Public Shared Event cardReadComplete(ByVal data As String)
 Public Sub kbHook_KeyDown(ByVal Key As Windows.Forms.Keys) 
  IO.File.AppendAllText("E:\log.log", Key.ToString() & vbCrLf)
 RaiseEvent cardReadComplete(encryptedData)
End Sub

Proj B

 Private Sub handleSwipeCardRequest(ByVal msgText As String)
        AddHandler CardReader.Main.cardReadComplete, AddressOf sendSwipeCardDetails
        CardReader.Main.cardReadComplete()
End Sub

I am calling handleSwipeCardRequest function first and then Raising its event.


回答1:


Your event will be raised when kbHook_KeyDown gets called, assuming it gets called after the AddHandler line is executed. Are you sure that the KeyDown function gets called? As Hans Passant said, you might be missing a Handles keyword:

Public Sub kbHook_KeyDown(ByVal Key As Windows.Forms.Keys) Handles kbHook.KeyDown
    ...
End Sub



回答2:


Another way :

AddHandler kbHook.KeyDown , AddressOf Me.kbHook_KeyDown


来源:https://stackoverflow.com/questions/7805496/how-can-i-raiseevent-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!