在 .net 示例中引发事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31844952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:42:16  来源:igfitidea点击:

Raising an event in .net example

.netvb.neteventsevent-handling

提问by Cary Bondoc

I am trying to create a basic example of raising an event in vb.net, and I am hoping that by studying it thoroughly I can upgrade the way my system receives data from serial port.

我正在尝试创建一个在 vb.net 中引发事件的基本示例,我希望通过彻底研究它可以升级我的系统从串行端口接收数据的方式。

Right now I have a system that receives the incoming data from serial port via timers, the problem is there are certain events in a system that conflicts in my timer. Because of this I am planning to change the way I received the data from serial ports, instead of timer I want to use vb.net raiseevent.

现在我有一个系统通过计时器从串行端口接收传入数据,问题是系统中的某些事件与我的计时器发生冲突。因此,我计划更改从串行端口接收数据的方式,而不是我想使用 vb.net raiseevent 的计时器。

Unfortunately I can't find a simple example on how to use this event, by searching thoroughly I saw the MSDN'spost about this topic and it is here. So, how do I use this example? I tried using it below like that

不幸的是,我找不到关于如何使用此事件的简单示例,通过彻底搜索,我看到了MSDN关于此主题帖子,它就在这里。那么,我该如何使用这个例子呢?我尝试在下面这样使用它

Public Event TimeExpired(ByVal Status As String)
Public Sub RaiseTimeExpiredEvent()
    RaiseEvent TimeExpired("Your time has run out")
    MessageBox.Show(TimeExpired())
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    RaiseTimeExpiredEvent()
End Sub

It's not working working, the error is

它不起作用,错误是

Error 1 'Public Event TimeExpired(Status As String)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. C:\Users\Cary\Desktop\Projects\Testing\Testing\Testing\Form1.vb 5 25 Testing

错误 1 ​​'Public Event TimeExpired(Status As String)' 是一个事件,不能直接调用。使用“RaiseEvent”语句引发事件。C:\Users\Cary\Desktop\Projects\Testing\Testing\Testing\Form1.vb 5 25 测试

Because of that error I tried to do it like this

由于那个错误,我试图这样做

Class Form1
Public Event TimeExpired(ByVal Status As String)
Public Sub RaiseTimeExpiredEvent()
    RaiseEvent TimeExpired("Your time has run out")
    MessageBox.Show(TimeExpired())

End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    RaiseEvent TimeExpired()
End Sub

End Class

结束类

But the error states

但错误指出

Error 2 Argument not specified for parameter 'Status' of 'Public Event TimeExpired(Status As String)'. C:\Users\Cary\Desktop\Projects\Testing\Testing\Testing\Form1.vb 11 9 Testing

未为“公共事件 TimeExpired(Status As String)”的参数“Status”指定错误 2 参数。C:\Users\Cary\Desktop\Projects\Testing\Testing\Testing\Form1.vb 11 9 测试

采纳答案by Drarig29

Are you using Visual Studio ? If yes, you can try to display the Error List. For that, click View, and Error List.

你在使用 Visual Studio 吗?如果是,您可以尝试显示错误列表。为此,单击查看和错误列表。

Class Form1

    Public Event TimeExpired(Status As String)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RaiseEvent TimeExpired("Your time has run out")
    End Sub

End Class

To handle the event, you can add this :

要处理该事件,您可以添加以下内容:

Private Sub HandleTimeExpired(Status As String) Handles Me.TimeExpired
    MsgBox(Status)
End Sub

Here is the full code :

这是完整的代码:

Class Form1

    Public Event TimeExpired(Status As String)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RaiseEvent TimeExpired("Your time has run out")
    End Sub

    Public Sub OnTimeExpired(Status As String) Handles Me.TimeExpired
        MsgBox(Status)
    End Sub

End Class

Edit :

编辑 :

If you wanna move the raising of event in a module, you can't. You have to add it in a class. See this link.

如果您想在模块中移动引发事件,则不能。您必须将其添加到类中。请参阅此链接

Example of Class1 (you should rename it...) :

Class1 示例(您应该将其重命名...):

Public Class Class1

    Private Event TimeExpired(Status As String)

    Public Sub OnTimeExpired(Status As String)
        RaiseEvent TimeExpired(Status)
    End Sub

    Private Sub HandleTimeExpired(Status As String) Handles Me.TimeExpired
       MsgBox(Status)
    End Sub

End Class

And to use it, you have to declare it WithEvents in your Form1 :

要使用它,您必须在 Form1 中声明它 WithEvents :

Public Class Form1

    Dim WithEvents Class1 As New Class1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Class1.OnTimeExpired("Your time has run out")
    End Sub

End Class