RaiseEvent

O

Onokiyo

Hello,

I have the code below and somehow the message from RaiseEvent doesn't
pop up at all. Can someone help me please?

'------CODE
'------/form1.vb/VB2005/Framework20---------

Imports System

Public Class Form1
Public Event TimeExpired(ByVal Status As String)

Public Sub RaiseTimeExpiredEvent()
RaiseEvent TimeExpired("Your time has run out")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RaiseTimeExpiredEvent()
End Sub
End Class

'-------END-----------------


Thank you very much.

P Nguyen
 
R

Robinson

'------CODE
'------/form1.vb/VB2005/Framework20---------

Imports System

Public Class Form1
Public Event TimeExpired(ByVal Status As String)

Public Sub RaiseTimeExpiredEvent()
RaiseEvent TimeExpired("Your time has run out")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
RaiseTimeExpiredEvent()
End Sub
End Class

The event is being raised, but it is not being "handled" by anything. You
need to add a handler for this event to be handled by the form that
originates it, which of course would be quite superfluous because you can
just as well handle the event in Button1_Click or RaiseTimeExpiredEvent.
The main use of raiseevent is to signal to a parent or owner object that
something has happened. For example, your button has raised a "Click" event
which has been "Handled" by your form.
 
O

Onokiyo

Spam said:
You need something to catch the event.

Hello Spam Catcher,

Isn't the RaiseEvent statement supposed to pop message on the screen
using the string parameter when it's being called?

According to your suggestion, if I'm not mistaken, are you saying that I
need to create an specific event handler to pop up a message?

I'm kinda new to VB.NET, please forgive my little understanding.

Thank you very much

P Nguyen
 
G

Guest

Hello Spam Catcher,

Isn't the RaiseEvent statement supposed to pop message on the screen
using the string parameter when it's being called?

No - MessageBox pops a message.

RaiseEvent fires the event.

According to your suggestion, if I'm not mistaken, are you saying that I
need to create an specific event handler to pop up a message?


public Sub MyEventHandler(Byval Message as string) handles me.MyEvent
Msgbox(Message)
End Sub


BTW, you can register event handlers dynamically using the code:

AddHandler Me.MyEvent, Addressof MyEventHandler
 
O

Onokiyo

Robinson said:
The event is being raised, but it is not being "handled" by anything. You
need to add a handler for this event to be handled by the form that
originates it, which of course would be quite superfluous because you can
just as well handle the event in Button1_Click or RaiseTimeExpiredEvent.
The main use of raiseevent is to signal to a parent or owner object that
something has happened. For example, your button has raised a "Click" event
which has been "Handled" by your form.

Hi Rob,

Your explanation is awesome! Thank you very much, I have it working now.

P Nguyen
 
O

Onokiyo

Spam said:
No - MessageBox pops a message.

RaiseEvent fires the event.




public Sub MyEventHandler(Byval Message as string) handles me.MyEvent
Msgbox(Message)
End Sub


BTW, you can register event handlers dynamically using the code:

AddHandler Me.MyEvent, Addressof MyEventHandler

Hi Spam Catcher,

Thank you for your technical help. I have the thing working now. You've
made my day although there is a thunderstorm outside right now. :)

Have a good day and happy Thanks Giving!

P Nguyen
 
P

Phill W.

Onokiyo said:
I have the code below and somehow the message from RaiseEvent doesn't
pop up at all. Can someone help me please?
Public Sub RaiseTimeExpiredEvent()
RaiseEvent TimeExpired("Your time has run out")
End Sub

I would strongly recommend you use the same Event model as Our Friends
in Redmond, specifically:

Public Event XYZ( ByVal sender As Object, ByVal e As EventArgs )

That way, the code handling any event always has a reference to the
object that sent it (in the sender argument) and you can create
subclasses of EventArgs as necessary to wrap up all the information that
you need to pass in the arguments (you can easily /add/ to these as time
goes on).

This is how I would put this together:

Public Class Form1

' The Event
Public Event TimeExpired( _
ByVal sender as Object _
, ByVal e as TimeExpiredEventArgs _
)

' The routine that raises the event; available to subclasses
Protected Sub OnTimeExpired()
Dim e As New TimeExpiredEventArgs

RaiseEvent TimeExpired( e )
End Sub

' Another version of same, passing a custom event argument
Protected Sub OnTimeExpired(ByVal e as TimeExpiredEventArgs)
RaiseEvent TimeExpired( e )
End Sub

' The Argument class, containing everything we need to pass
Public Class TimeExpiredEventArgs
Inherits EventArgs

' Only THIS assembly can create these; nobody else
' Leave this out and you'll get a default, Public Constructor!
Friend Sub New()
MyBase.New()
End Sub

' This is the message we're passing ...
Public ReadOnly Property Message() as String
Get
Return m_sMessage
End Get
End Property

' ... and this is we it's stored internally
Private m_sMessage as String = "Your time has run out"

End Class

End Class

Then, elsewhere, you can handle this event using;

Private Sub Thing_TimeExpired( _
ByVal sender As Object _
, ByVal e As TimeExpiredEventArgs _
) Handles Thing.TimeExpired

MsgBox( e.Message )

End Sub


OK, this is probably *way* over the top for your application, but using
sender and eventargs is definitely the way to go...

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top