By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

RaiseEvent

P: n/a
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
Nov 16 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Onokiyo <on*****@gmail.comwrote in news:#CusKzZCHHA.3660
@TK2MSFTNGP06.phx.gbl:
I have the code below and somehow the message from RaiseEvent doesn't
pop up at all. Can someone help me please?
You need something to catch the event.
Nov 16 '06 #2

P: n/a
'------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.
Nov 16 '06 #3

P: n/a
Spam Catcher wrote:
Onokiyo <on*****@gmail.comwrote in news:#CusKzZCHHA.3660
@TK2MSFTNGP06.phx.gbl:
>I have the code below and somehow the message from RaiseEvent doesn't
pop up at all. Can someone help me please?

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
Nov 16 '06 #4

P: n/a
Onokiyo <on*****@gmail.comwrote in news:u2NfxIaCHHA.3380
@TK2MSFTNGP04.phx.gbl:
Spam Catcher wrote:
>Onokiyo <on*****@gmail.comwrote in news:#CusKzZCHHA.3660
@TK2MSFTNGP06.phx.gbl:
>>I have the code below and somehow the message from RaiseEvent
doesn't
>>pop up at all. Can someone help me please?

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?
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
Nov 16 '06 #5

P: n/a
Robinson wrote:
>'------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.

Hi Rob,

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

P Nguyen
Nov 16 '06 #6

P: n/a
Spam Catcher wrote:
Onokiyo <on*****@gmail.comwrote in news:u2NfxIaCHHA.3380
@TK2MSFTNGP04.phx.gbl:
>Spam Catcher wrote:
>>Onokiyo <on*****@gmail.comwrote in news:#CusKzZCHHA.3660
@TK2MSFTNGP06.phx.gbl:

I have the code below and somehow the message from RaiseEvent
doesn't
>>>pop up at all. Can someone help me please?
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?

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
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
Nov 16 '06 #7

P: n/a
Onokiyo wrote:
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.
Nov 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.