RaiseEvent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Onokiyo

    #1

    RaiseEvent

    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(ByV al Status As String)

    Public Sub RaiseTimeExpire dEvent()
    RaiseEvent TimeExpired("Yo ur time has run out")
    End Sub

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    RaiseTimeExpire dEvent()
    End Sub
    End Class

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


    Thank you very much.

    P Nguyen
  • Spam Catcher

    #2
    Re: RaiseEvent

    Onokiyo <onokiyo@gmail. comwrote in news:#CusKzZCHH A.3660
    @TK2MSFTNGP06.p hx.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.

    Comment

    • Robinson

      #3
      Re: RaiseEvent

      '------CODE
      '------/form1.vb/VB2005/Framework20---------
      >
      Imports System
      >
      Public Class Form1
      Public Event TimeExpired(ByV al Status As String)
      >
      Public Sub RaiseTimeExpire dEvent()
      RaiseEvent TimeExpired("Yo ur time has run out")
      End Sub
      >
      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      RaiseTimeExpire dEvent()
      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 RaiseTimeExpire dEvent.
      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.


      Comment

      • Onokiyo

        #4
        Re: RaiseEvent

        Spam Catcher wrote:
        Onokiyo <onokiyo@gmail. comwrote in news:#CusKzZCHH A.3660
        @TK2MSFTNGP06.p hx.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

        Comment

        • Spam Catcher

          #5
          Re: RaiseEvent

          Onokiyo <onokiyo@gmail. comwrote in news:u2NfxIaCHH A.3380
          @TK2MSFTNGP04.p hx.gbl:
          Spam Catcher wrote:
          >Onokiyo <onokiyo@gmail. comwrote in news:#CusKzZCHH A.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

          Comment

          • Onokiyo

            #6
            Re: RaiseEvent

            Robinson wrote:
            >'------CODE
            >'------/form1.vb/VB2005/Framework20---------
            >>
            >Imports System
            >>
            >Public Class Form1
            > Public Event TimeExpired(ByV al Status As String)
            >>
            > Public Sub RaiseTimeExpire dEvent()
            > RaiseEvent TimeExpired("Yo ur time has run out")
            > End Sub
            >>
            > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
            >System.EventAr gs) Handles Button1.Click
            > RaiseTimeExpire dEvent()
            > 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 RaiseTimeExpire dEvent.
            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

            Comment

            • Onokiyo

              #7
              Re: RaiseEvent

              Spam Catcher wrote:
              Onokiyo <onokiyo@gmail. comwrote in news:u2NfxIaCHH A.3380
              @TK2MSFTNGP04.p hx.gbl:
              >
              >Spam Catcher wrote:
              >>Onokiyo <onokiyo@gmail. comwrote in news:#CusKzZCHH A.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

              Comment

              • Phill W.

                #8
                Re: RaiseEvent

                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 RaiseTimeExpire dEvent()
                RaiseEvent TimeExpired("Yo ur 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 TimeExpiredEven tArgs _
                )

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

                RaiseEvent TimeExpired( e )
                End Sub

                ' Another version of same, passing a custom event argument
                Protected Sub OnTimeExpired(B yVal e as TimeExpiredEven tArgs)
                RaiseEvent TimeExpired( e )
                End Sub

                ' The Argument class, containing everything we need to pass
                Public Class TimeExpiredEven tArgs
                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_TimeExpir ed( _
                ByVal sender As Object _
                , ByVal e As TimeExpiredEven tArgs _
                ) Handles Thing.TimeExpir ed

                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.

                Comment

                Working...