Raising events from a class

S

SetonSoftware

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
 
M

Mayur H Chauhan

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.EventArgs)

within the shared method call

RaiseEvent E1(argument that you need to pass)

Thanks,
Mayur Chauhan
 
S

SetonSoftware

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.EventArgs)

within the shared method call

RaiseEvent E1(argument that you need to pass)

Thanks,






- Show quoted text -

Maybe if I showed some test code the question would make more sense.
I'm invoking a shared method here:

MenuManager1.TestIt()

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
 
Z

zacks

Maybe if I showed some test code the question would make more sense.
I'm invoking a shared method here:

MenuManager1.TestIt()

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.
 
S

SetonSoftware

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.Start itself, something like this:

    Private Sub Form1_Load(ByVal sender As Object, _
 ByVal e As System.EventArgs) Handles Me.Load

        AddHandler MenuManager1.Start, AddressOf MyStartHandler

    End Sub

    Private Sub MyStartHandler(ByVal y As Integer)
        MsgBox(y.ToString)
    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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AddHandler MenuManager1.Start, AddressOf MySender_Start

MenuManager1.TestIt()

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
 
Z

zacks

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

  Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

    AddHandler MenuManager1.Start, AddressOf MySender_Start

    MenuManager1.TestIt()

  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.
 

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