Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / May 2008

Tip: Looking for answers? Try searching our database.

Raising events from a class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SetonSoftware - 21 May 2008 18:32 GMT
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
Kerry Moorman - 21 May 2008 19:17 GMT
Carl,

You can use AddHandler instead of WithEvents.

Kerry Moorman

> I have a class with shared properties and methods that manages the
> open forms in my WinForms application, VB.NET 2005. The data contained
[quoted text clipped - 10 lines]
>
> Carl
Mayur H Chauhan - 21 May 2008 19:19 GMT
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
>I have a class with shared properties and methods that manages the
> open forms in my WinForms application, VB.NET 2005. The data contained
[quoted text clipped - 10 lines]
>
> Carl
SetonSoftware - 21 May 2008 19:58 GMT
> Carl,
>          Within the Shared class you can define Shared event and that event
[quoted text clipped - 25 lines]
>
> - 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
zacks@construction-imaging.com - 21 May 2008 21:49 GMT
> > Carl,
> >          Within the Shared class you can define Shared event and that event
[quoted text clipped - 68 lines]
>
> 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.
Steve Gerrard - 22 May 2008 06:35 GMT
> I want the MySender_Start() event handler method to fire when the
> RaiseEvent is invoked. The problem is that this is not happening. The
[quoted text clipped - 16 lines]
>
> 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.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
SetonSoftware - 22 May 2008 14:38 GMT
> > I want the MySender_Start() event handler method to fire when the
> > RaiseEvent is invoked. The problem is that this is not happening. The
[quoted text clipped - 34 lines]
>
> - 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
zacks@construction-imaging.com - 22 May 2008 20:36 GMT
> > > I want the MySender_Start() event handler method to fire when the
> > > RaiseEvent is invoked. The problem is that this is not happening. The
[quoted text clipped - 65 lines]
>
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.