473,473 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jun 27 '08 #1
7 1839
Carl,

You can use AddHandler instead of WithEvents.

Kerry Moorman
"SetonSoftware" 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
Jun 27 '08 #2
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
"SetonSoftware" <se************@verizon.netwrote in message
news:52**********************************@i76g2000 hsf.googlegroups.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

Jun 27 '08 #3
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.EventArgs)

within the shared method call

RaiseEvent E1(argument that you need to pass)

Thanks,
Mayur Chauhan"SetonSoftware" <seton.softw...@verizon.netwrote in message

news:52**********************************@i76g2000 hsf.googlegroups.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.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
Jun 27 '08 #4
On May 21, 2:58*pm, SetonSoftware <seton.softw...@verizon.netwrote:
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.EventArgs)
within the shared method call
RaiseEvent E1(argument that you need to pass)
Thanks,
Mayur Chauhan"SetonSoftware" <seton.softw...@verizon.netwrote in message
news:52**********************************@i76g2000 hsf.googlegroups.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.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.
Jun 27 '08 #5
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.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

Jun 27 '08 #6
On May 22, 1:35*am, "Steve Gerrard" <mynameh...@comcast.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.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
Jun 27 '08 #7
On May 22, 9:38*am, SetonSoftware <seton.softw...@verizon.netwrote:
On May 22, 1:35*am, "Steve Gerrard" <mynameh...@comcast.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.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
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.
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: serge calderara | last post by:
Dear all, I have a class wich is raising events as normally it should do. having a form in the same assembly wich is catching those events works fne. Raise events gets catch normaly within the...
2
by: Wavemaker | last post by:
I have a component class that has a set of events. The class uses a worker thread to do some processing. When it is finished with the processing, it raises the appropriate events. Problem,...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
7
by: cider123 | last post by:
I'm coding a project using the following article as reference: http://www.codeproject.com/csharp/DynamicPluginManager.asp In this type of project, plugins are loaded dynamically into a Plugin...
2
by: Catherine Jones | last post by:
Dear All Please help me in the following issue: how to raise an event from an unmanaged class to a managed class. Both classes are in C++ and there should not be any static methods involved in...
0
by: Greg Park | last post by:
I have many user controls loading into a web page using Page.LoadControl However, I'm unable to figure out how to raise an event when a button is click or a check box is checked. I can have...
0
by: Joe Campbell | last post by:
I am encountering a problem raising WMI events from an asp.net application. The error received (as captured in the event log) is as follows: System.Runtime.InteropServices.COMException...
4
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.