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.
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.
Unfortunately I can't find a simple example on how to use this event, by searching thoroughly I saw the MSDN's post about this topic and it is here. So, how do I use this example? I tried using it below like that
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
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
Are you using Visual Studio ? If yes, you can try to display the Error List. For that, click View, and Error List.
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...) :
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 :
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With