Raising events from a class

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

    Raising events from a class

    I have a class with shared properties and methods that manages the
    open forms in my WinForms application, VB.NET 2005. The data contained
    within must persist for the lifetime of the application. When the user
    selects a menu item on the main form I want the method of the class to
    add the form's data to the internal HashTable that stores it and then
    raise an event in the main form that will load a dialog form.

    Since the class is never instantiated I don't see how I can use
    WithEvents to handle publish/subscribe. Any ideas as to how I can get
    the menu manager class to do this?

    Thanks

    Carl
  • =?Utf-8?B?S2VycnkgTW9vcm1hbg==?=

    #2
    RE: Raising events from a class

    Carl,

    You can use AddHandler instead of WithEvents.

    Kerry Moorman


    "SetonSoftw are" wrote:
    I have a class with shared properties and methods that manages the
    open forms in my WinForms application, VB.NET 2005. The data contained
    within must persist for the lifetime of the application. When the user
    selects a menu item on the main form I want the method of the class to
    add the form's data to the internal HashTable that stores it and then
    raise an event in the main form that will load a dialog form.
    >
    Since the class is never instantiated I don't see how I can use
    WithEvents to handle publish/subscribe. Any ideas as to how I can get
    the menu manager class to do this?
    >
    Thanks
    >
    Carl
    >

    Comment

    • Mayur H Chauhan

      #3
      Re: Raising events from a class

      Carl,
      Within the Shared class you can define Shared event and that event
      can be raised from your shared method.

      Here is an example Declare event for the shared class.
      Shared Event E1(byval e as System.EventArg s)

      within the shared method call

      RaiseEvent E1(argument that you need to pass)

      Thanks,
      Mayur Chauhan
      "SetonSoftw are" <seton.software @verizon.netwro te in message
      news:52da3033-2968-482d-bbaf-bc947c7e3a65@i7 6g2000hsf.googl egroups.com...
      >I have a class with shared properties and methods that manages the
      open forms in my WinForms application, VB.NET 2005. The data contained
      within must persist for the lifetime of the application. When the user
      selects a menu item on the main form I want the method of the class to
      add the form's data to the internal HashTable that stores it and then
      raise an event in the main form that will load a dialog form.
      >
      Since the class is never instantiated I don't see how I can use
      WithEvents to handle publish/subscribe. Any ideas as to how I can get
      the menu manager class to do this?
      >
      Thanks
      >
      Carl

      Comment

      • SetonSoftware

        #4
        Re: Raising events from a class

        On May 21, 2:19 pm, "Mayur H Chauhan" <ma...@orioninc .comwrote:
        Carl,
                 Within the Shared class you can define Shared event andthat event
        can be raised from your shared method.
        >
        Here is an example Declare event for the shared class.
        Shared Event E1(byval e as System.EventArg s)
        >
        within the shared method call
        >
        RaiseEvent E1(argument that you need to pass)
        >
        Thanks,
        Mayur Chauhan"SetonSo ftware" <seton.softw... @verizon.netwro te in message
        >
        news:52da3033-2968-482d-bbaf-bc947c7e3a65@i7 6g2000hsf.googl egroups.com...
        >
        >
        >
        I have a class with shared properties and methods that manages the
        open forms in my WinForms application, VB.NET 2005. The data contained
        within must persist for the lifetime of the application. When the user
        selects a menu item on the main form I want the method of the class to
        add the form's data to the internal HashTable that stores it and then
        raise an event in the main form that will load a dialog form.
        >
        Since the class is never instantiated I don't see how I can use
        WithEvents to handle publish/subscribe. Any ideas as to how I can get
        the menu manager class to do this?
        >
        Thanks
        >
        Carl- Hide quoted text -
        >
        - Show quoted text -
        Maybe if I showed some test code the question would make more sense.
        I'm invoking a shared method here:

        MenuManager1.Te stIt()

        This event is executing a method which some some work and raises and
        event here:

        Public Class MenuManager1
        Inherits System.Object

        Public Shared Event Start(ByVal y As Int32)

        Public Shared Sub TestIt()
        RaiseEvent Start(123)
        End Sub

        End Class


        I want the MySender_Start( ) event handler method to fire when the
        RaiseEvent is invoked. The problem is that this is not happening. The
        testIt event just finishes executong withou firing MySender_Start( ) .
        No error occurs.

        Public Class MyForm

        Private WithEvents MySender As MenuManager1

        Private Sub MySender_Start( ByVal y As Int32) Handles MySender.Start

        Dim x As Int32

        x = 1

        End Sub

        End Class

        What am I missing here?

        Thanks again

        Carl

        Comment

        • zacks@construction-imaging.com

          #5
          Re: Raising events from a class

          On May 21, 2:58 pm, SetonSoftware <seton.softw... @verizon.netwro te:
          On May 21, 2:19 pm, "Mayur H Chauhan" <ma...@orioninc .comwrote:
          >
          >
          >
          >
          >
          Carl,
                   Within the Shared class you can define Shared event and that event
          can be raised from your shared method.
          >
          Here is an example Declare event for the shared class.
          Shared Event E1(byval e as System.EventArg s)
          >
          within the shared method call
          >
          RaiseEvent E1(argument that you need to pass)
          >
          Thanks,
          Mayur Chauhan"SetonSo ftware" <seton.softw... @verizon.netwro te in message
          >
          news:52da3033-2968-482d-bbaf-bc947c7e3a65@i7 6g2000hsf.googl egroups.com...
          >
          >I have a class with shared properties and methods that manages the
          open forms in my WinForms application, VB.NET 2005. The data contained
          within must persist for the lifetime of the application. When the user
          selects a menu item on the main form I want the method of the class to
          add the form's data to the internal HashTable that stores it and then
          raise an event in the main form that will load a dialog form.
          >
          Since the class is never instantiated I don't see how I can use
          WithEvents to handle publish/subscribe. Any ideas as to how I can get
          the menu manager class to do this?
          >
          Thanks
          >
          Carl- Hide quoted text -
          >
          - Show quoted text -
          >
          Maybe if I showed some test code the question would make more sense.
          I'm invoking a shared method here:
          >
          MenuManager1.Te stIt()
          >
          This event is executing  a method which some some work and raises and
          event here:
          >
          Public Class MenuManager1
            Inherits System.Object
          >
            Public Shared Event Start(ByVal y As Int32)
          >
            Public Shared Sub TestIt()
              RaiseEvent Start(123)
            End Sub
          >
          End Class
          >
          I want the MySender_Start( ) event handler method to fire when the
          RaiseEvent is invoked. The problem is that this is not happening. The
          testIt event just finishes executong withou firing MySender_Start( ) .
          No error occurs.
          >
          Public Class MyForm
          >
            Private WithEvents MySender As MenuManager1
          >
            Private Sub MySender_Start( ByVal y As Int32) Handles MySender.Start
          >
              Dim x As Int32
          >
              x = 1
          >
            End Sub
          >
          End Class
          >
          What am I missing here?
          The event handler in the form class needs to be Public, not private,
          since it is being invoked from a different class object.

          Comment

          • Steve Gerrard

            #6
            Re: Raising events from a class

            SetonSoftware wrote:
            >
            I want the MySender_Start( ) event handler method to fire when the
            RaiseEvent is invoked. The problem is that this is not happening. The
            testIt event just finishes executong withou firing MySender_Start( ) .
            No error occurs.
            >
            Public Class MyForm
            >
            Private WithEvents MySender As MenuManager1
            >
            Private Sub MySender_Start( ByVal y As Int32) Handles MySender.Start
            >
            Dim x As Int32
            >
            x = 1
            >
            End Sub
            >
            End Class
            >
            What am I missing here?
            >
            Your missing that MySender would be an instance, not the shared class itself,
            and that it is Nothing, since you never create an instance.

            You want to handle MenuManager1.St art itself, something like this:

            Private Sub Form1_Load(ByVa l sender As Object, _
            ByVal e As System.EventArg s) Handles Me.Load

            AddHandler MenuManager1.St art, AddressOf MyStartHandler

            End Sub

            Private Sub MyStartHandler( ByVal y As Integer)
            MsgBox(y.ToStri ng)
            End Sub



            Comment

            • SetonSoftware

              #7
              Re: Raising events from a class

              On May 22, 1:35 am, "Steve Gerrard" <mynameh...@com cast.netwrote:
              SetonSoftware wrote:
              >
              I want the MySender_Start( ) event handler method to fire when the
              RaiseEvent is invoked. The problem is that this is not happening. The
              testIt event just finishes executong withou firing MySender_Start( ) .
              No error occurs.
              >
              Public Class MyForm
              >
                Private WithEvents MySender As MenuManager1
              >
                Private Sub MySender_Start( ByVal y As Int32) Handles MySender.Start
              >
                  Dim x As Int32
              >
                  x = 1
              >
                End Sub
              >
              End Class
              >
              What am I missing here?
              >
              Your missing that MySender would be an instance, not the shared class itself,
              and that it is Nothing, since you never create an instance.
              >
              You want to handle MenuManager1.St art itself, something like this:
              >
                  Private Sub Form1_Load(ByVa l sender As Object, _
               ByVal e As System.EventArg s) Handles Me.Load
              >
                      AddHandler MenuManager1.St art, AddressOf MyStartHandler
              >
                  End Sub
              >
                  Private Sub MyStartHandler( ByVal y As Integer)
                      MsgBox(y.ToStri ng)
                  End Sub- Hide quoted text -
              >
              - Show quoted text -


              Many thanks. And for the benefit of others here is the full working
              solution:

              Private Sub frmMain_Load(By Val sender As System.Object, ByVal e As
              System.EventArg s) Handles MyBase.Load

              AddHandler MenuManager1.St art, AddressOf MySender_Start

              MenuManager1.Te stIt()

              End Sub

              Public Sub MySender_Start( ByVal y As Int32)

              Dim x As Int32

              x = 1

              End Sub

              Public Class MenuManager1
              Inherits System.Object

              Public Shared Event Start(ByVal y As Int32)

              Public Shared Sub TestIt()
              RaiseEvent Start(123)
              End Sub

              End Class

              Comment

              • zacks@construction-imaging.com

                #8
                Re: Raising events from a class

                On May 22, 9:38 am, SetonSoftware <seton.softw... @verizon.netwro te:
                On May 22, 1:35 am, "Steve Gerrard" <mynameh...@com cast.netwrote:
                >
                >
                >
                >
                >
                SetonSoftware wrote:
                >
                I want the MySender_Start( ) event handler method to fire when the
                RaiseEvent is invoked. The problem is that this is not happening. The
                testIt event just finishes executong withou firing MySender_Start( ) .
                No error occurs.
                >
                Public Class MyForm
                >
                  Private WithEvents MySender As MenuManager1
                >
                  Private Sub MySender_Start( ByVal y As Int32) Handles MySender.Start
                >
                    Dim x As Int32
                >
                    x = 1
                >
                  End Sub
                >
                End Class
                >
                What am I missing here?
                >
                Your missing that MySender would be an instance, not the shared class itself,
                and that it is Nothing, since you never create an instance.
                >
                You want to handle MenuManager1.St art itself, something like this:
                >
                    Private Sub Form1_Load(ByVa l sender As Object, _
                 ByVal e As System.EventArg s) Handles Me.Load
                >
                        AddHandler MenuManager1.St art, AddressOf MyStartHandler
                >
                    End Sub
                >
                    Private Sub MyStartHandler( ByVal y As Integer)
                        MsgBox(y.ToStri ng)
                    End Sub- Hide quoted text -
                >
                - Show quoted text -
                >
                Many thanks. And for the benefit of others here is the full working
                solution:
                >
                  Private Sub frmMain_Load(By Val sender As System.Object, ByVal e As
                System.EventArg s) Handles MyBase.Load
                >
                    AddHandler MenuManager1.St art, AddressOf MySender_Start
                >
                    MenuManager1.Te stIt()
                >
                  End Sub
                >
                  Public Sub MySender_Start( ByVal y As Int32)
                >
                    Dim x As Int32
                >
                    x = 1
                >
                  End Sub
                >
                Public Class MenuManager1
                  Inherits System.Object
                >
                  Public Shared Event Start(ByVal y As Int32)
                >
                  Public Shared Sub TestIt()
                    RaiseEvent Start(123)
                  End Sub
                >
                End Class
                You didn't have to change from defining the event hander with the
                HANDLES keyword to an ADDHANDLER statement. Either way will work, it's
                your own preference.

                Comment

                Working...