如何在 VB.net 中引发事件 [英] How can I RaiseEvent in VB.net

查看:26
本文介绍了如何在 VB.net 中引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案中有 2 个项目.
让我们说 Proj A 和 Proj B.

Proj A 有我的自定义事件.并且相同的 Proj 正在使用 Vb.net 的 RaiseEvent 函数引发该事件,而 Proj B 正在参考 Proj A.
Proj B 正在为 Proj A 的自定义事件添加处理程序.

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.?

项目 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

项目 B

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

我首先调用 handleSwipeCardRequest 函数,然后引发其事件.

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

推荐答案

您的事件将在 kbHook_KeyDown 被调用时引发,假设它在 AddHandler 行之后被调用执行.您确定调用了 KeyDown 函数吗?正如 Hans Passant 所说,您可能缺少 Handles 关键字:

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

这篇关于如何在 VB.net 中引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆