473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Connecting an event handler in one class and disconnecting it in a different class.

In my code, class A instanciates classes B and C.

I would like class B to connect an event handler to a method in class A, and
for class C to disconnect that event handler.

I think I've done too much thinking, and now I'm even more confused as to
how to accomplish this as when I started.

Any help will be appreciated.
Nov 15 '05 #1
10 3468
Hi,

Thanks for posting. The following code is for your reference:

using System;

public delegate void ADelegate();

public class A
{
public static void Main(string[] args)
{
B b = new B();
ADelegate d = new ADelegate(AMethod);
b.AEvent += d;
C c = new C();
c.RemoveDelegateFromB(d, b);
}

public static void AMethod()
{
Console.WriteLine("A method");
}
}

public class B
{
public event ADelegate AEvent;
public void RaiseAEvent()
{
if (AEvent != null)
{
AEvent();
}
}
}

public class C
{
public void RemoveDelegateFromB(ADelegate d, B b)
{
b.AEvent -= d;
}
}

I hope this helps. If there is anything else I can help with, please feel
free to post here.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #2
Assuming that ClassB and ClassC have a reference to ClassA then there should
be no Problem in doing this.

class ClassA
{
ClassB cl_B=null;
ClassC cl_C=null;
Panel pn_Panel=null
public ClassA()
{
pn_Panel = new Panel();
cl_B = new ClassB(this);
cl_C = new ClassC(this);
}
public void Dispose()
{
if (cl_C != null)
cl_C=null;
}
protected virtual void OnMouseDown(object
sender,System.Windows.Forms.MouseEventArgs e)
{
if (i_DragDrop == 0)
{
OnDragDrop(e.X,e.Y); // Do what must be done and check the results, set
i_DragDrop if something found
} // if (i_DragDrop == 0)
} // protected override void OnMouseDown(object
sender,System.Windows.Forms.MouseEventArgs e)
} // Class A

class ClassB
{
public ClassB(ClassA cl_A)
{
cl_A.pn_Panel.MouseDown += new
System.Windows.Forms.MouseEventHandler(cl_A.OnMous eDown);
}
} // ClassB
class ClassC
{
ClassA cl_A=null;
public ClassC(ClassA clA)
{
cl_A = clA;
}
public void Dispose()
{
if (cl_A != null)
cl_A.pn_Panel.MouseDown -= new
System.Windows.Forms.MouseEventHandler(cl_A.OnMous eDown);
}
} // ClassC

This should work.
This works in my Projects where a ClassA calls ClassB assuming that ClassB
will clean up what it has done in ClassA when closing.
Note : since ClassA is only needed in the Construction of ClassB here, it is
not saved to a Class Field as in ClassC.

Mark Johnson, Berlin Germany
mj*****@mj10777.de

"SunshineGirl" <bl**@blah.com> schrieb im Newsbeitrag
news:10*************@corp.supernews.com...
In my code, class A instanciates classes B and C.

I would like class B to connect an event handler to a method in class A, and for class C to disconnect that event handler.

I think I've done too much thinking, and now I'm even more confused as to
how to accomplish this as when I started.

Any help will be appreciated.

Nov 15 '05 #3
Not quite.

Class A must hold the code for the event.
Class B must connect an event handler (+=), not raise the event.
Class C must disconnect the event handler (-=).

This is an already running application to which I'm trying to add
functionality. Class B uses Windows instrumentation to receive a
notification when the user launches an application. Class C uses Windows
instrumentation to receive a notification when the user terminates an
application. Class A instanciates both classes B and C. Classes B and C
don't know about each other.

The application currently monitors when the user has launched or terminated
applications. I'm trying to add the following functionality.

Whenever class B receives notification that Internet Explorer has launched,
it needs to connect the BeforeNavigate2 event handler to a method in class A
(so class B must do the += thing: ie.BeforeNavigate2 += new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(this.ie_BeforeNavigat
e2). Whenever class C receives notification that Internet Explorer has been
terminated, it needs to disconnect the BeforeNavigate2 event handler (so
class C must do the -= thing: ie.BeforeNavigate2 -= new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(this.ie_BeforeNavigat
e2).

"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:sT*************@cpmsftngxa07.phx.gbl...
Hi,

Thanks for posting. The following code is for your reference:

using System;

public delegate void ADelegate();

public class A
{
public static void Main(string[] args)
{
B b = new B();
ADelegate d = new ADelegate(AMethod);
b.AEvent += d;
C c = new C();
c.RemoveDelegateFromB(d, b);
}

public static void AMethod()
{
Console.WriteLine("A method");
}
}

public class B
{
public event ADelegate AEvent;
public void RaiseAEvent()
{
if (AEvent != null)
{
AEvent();
}
}
}

public class C
{
public void RemoveDelegateFromB(ADelegate d, B b)
{
b.AEvent -= d;
}
}

I hope this helps. If there is anything else I can help with, please feel
free to post here.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4
Hello,

Thanks for your update.

I would like to ask a question. What is the object that exposes the event
"BeforeNavigate2" and how do you get a reference to it? If we can get the
reference (e.g. named "o") successfully, we can simply call
"o.BeforeNavigate2 += " in class B and "o.BeforeNavigate2 -= " in class C.

In addition, since the method for the event is defined in class A, we
cannot use "this" in class B and class C. If we define the method as
static, we can use "o.BeforeNavigate2 += new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(A.ie_BeforeNavigate2)
". If the method is non-static, we need to pass a reference to A into B or
simply create a new A object, so that the method can be accessed.

I hope this helps.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5
I should have thought of that.

Thanks for the response.
"Mark Johnson" <mj*****@mj10777.de> wrote in message
news:40**********************@newsread2.arcor-online.net...
Assuming that ClassB and ClassC have a reference to ClassA then there should be no Problem in doing this.

class ClassA
{
ClassB cl_B=null;
ClassC cl_C=null;
Panel pn_Panel=null
public ClassA()
{
pn_Panel = new Panel();
cl_B = new ClassB(this);
cl_C = new ClassC(this);
}
public void Dispose()
{
if (cl_C != null)
cl_C=null;
}
protected virtual void OnMouseDown(object
sender,System.Windows.Forms.MouseEventArgs e)
{
if (i_DragDrop == 0)
{
OnDragDrop(e.X,e.Y); // Do what must be done and check the results, set i_DragDrop if something found
} // if (i_DragDrop == 0)
} // protected override void OnMouseDown(object
sender,System.Windows.Forms.MouseEventArgs e)
} // Class A

class ClassB
{
public ClassB(ClassA cl_A)
{
cl_A.pn_Panel.MouseDown += new
System.Windows.Forms.MouseEventHandler(cl_A.OnMous eDown);
}
} // ClassB
class ClassC
{
ClassA cl_A=null;
public ClassC(ClassA clA)
{
cl_A = clA;
}
public void Dispose()
{
if (cl_A != null)
cl_A.pn_Panel.MouseDown -= new
System.Windows.Forms.MouseEventHandler(cl_A.OnMous eDown);
}
} // ClassC

This should work.
This works in my Projects where a ClassA calls ClassB assuming that ClassB will clean up what it has done in ClassA when closing.
Note : since ClassA is only needed in the Construction of ClassB here, it is not saved to a Class Field as in ClassC.

Mark Johnson, Berlin Germany
mj*****@mj10777.de

"SunshineGirl" <bl**@blah.com> schrieb im Newsbeitrag
news:10*************@corp.supernews.com...
In my code, class A instanciates classes B and C.

I would like class B to connect an event handler to a method in class A,

and
for class C to disconnect that event handler.

I think I've done too much thinking, and now I'm even more confused as to how to accomplish this as when I started.

Any help will be appreciated.


Nov 15 '05 #6
The object that exposes the BeforeNavigate2 event is the Internet Explorer
ShellWindows interface.

Here is that part of the code that connects the event handler. This is from
a Windows app and it works. Now it must be in class B):
private static SHDocVw.ShellWindows shellWindows = new
SHDocVw.ShellWindowsClass();

foreach(SHDocVw.InternetExplorer ie in shellWindows)
ie.BeforeNavigate2 += new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(this.ie_BeforeNavigat
e2);

This is the event handler (that must be in class A):
public void ie_BeforeNavigate2(object pDisp , ref object url, ref object
Flags, ref object TargetFrameName, ref object PostData, ref object
Headers, ref bool Cancel)
{
MessageBox.Show("BeforeNavigate2: " + url.ToString());
}

Thank you for your help.
"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:Qz**************@cpmsftngxa07.phx.gbl...
Hello,

Thanks for your update.

I would like to ask a question. What is the object that exposes the event
"BeforeNavigate2" and how do you get a reference to it? If we can get the
reference (e.g. named "o") successfully, we can simply call
"o.BeforeNavigate2 += " in class B and "o.BeforeNavigate2 -= " in class C.

In addition, since the method for the event is defined in class A, we
cannot use "this" in class B and class C. If we define the method as
static, we can use "o.BeforeNavigate2 += new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(A.ie_BeforeNavigate2) ". If the method is non-static, we need to pass a reference to A into B or
simply create a new A object, so that the method can be accessed.

I hope this helps.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #7
Here's how far I got. But it doesn't work. Class B never enters the foreach loop.

ClassA:
public static SHDocVw.ShellWindows shellWindows = null;

public void ie_BeforeNavigate2(object disp, ref object url, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
{
MessageBox.Show("BeforeNavigate2: " + url.ToString());
}

ClassB:
private ClassA classA = null;

in the constructor:
ClassA.shellWindows = new SHDocVw.ShellWindowsClass();
foreach(SHDocVw.InternetExplorer ie in ClassA.shellWindows)
ie.BeforeNavigate2 += new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(classA.ie_BeforeNavigate2);

ClassC:
private ClassA classA = null;

in the constructor:
ClassA.shellWindows = new SHDocVw.ShellWindowsClass();
foreach(SHDocVw.InternetExplorer ie in ClassA.shellWindows)
ie.BeforeNavigate2 -= new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(classA.ie_BeforeNavigate2);


"SunshineGirl" <bl**@blah.com> wrote in message news:10*************@corp.supernews.com...
The object that exposes the BeforeNavigate2 event is the Internet Explorer
ShellWindows interface.

Here is that part of the code that connects the event handler. This is from
a Windows app and it works. Now it must be in class B):
private static SHDocVw.ShellWindows shellWindows = new
SHDocVw.ShellWindowsClass();

foreach(SHDocVw.InternetExplorer ie in shellWindows)
ie.BeforeNavigate2 += new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(this.ie_BeforeNavigat
e2);

This is the event handler (that must be in class A):
public void ie_BeforeNavigate2(object pDisp , ref object url, ref object
Flags, ref object TargetFrameName, ref object PostData, ref object
Headers, ref bool Cancel)
{
MessageBox.Show("BeforeNavigate2: " + url.ToString());
}

Thank you for your help.


"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:Qz**************@cpmsftngxa07.phx.gbl...
Hello,

Thanks for your update.

I would like to ask a question. What is the object that exposes the event
"BeforeNavigate2" and how do you get a reference to it? If we can get the
reference (e.g. named "o") successfully, we can simply call
"o.BeforeNavigate2 += " in class B and "o.BeforeNavigate2 -= " in class C.

In addition, since the method for the event is defined in class A, we
cannot use "this" in class B and class C. If we define the method as
static, we can use "o.BeforeNavigate2 += new

SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(A.ie_BeforeNavigate2)
". If the method is non-static, we need to pass a reference to A into B or
simply create a new A object, so that the method can be accessed.

I hope this helps.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Nov 15 '05 #8
Hello,

Thanks for your update. Let's try the following:

ClassA:
public static SHDocVw.ShellWindows shellWindows = new
SHDocVw.ShellWindowsClass();

public static void ie_BeforeNavigate2(object disp, ref object url, ref
object flags, ref object targetFrameName, ref object postData, ref object
headers, ref bool cancel)
{
MessageBox.Show("BeforeNavigate2: " + url.ToString());
}

ClassB:

in the constructor

foreach(SHDocVw.InternetExplorer ie in ClassA.shellWindows)
ie.BeforeNavigate2 += new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(ClassA.ie_BeforeNavig
ate2);

ClassC:

in the constructor

foreach(SHDocVw.InternetExplorer ie in ClassA.shellWindows)
ie.BeforeNavigate2 -= new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(ClassA.ie_BeforeNavig
ate2);

Since the "shellWindows" is a static member, we can access it from both
ClassB and ClassC. We only need to "new" it once in ClassA. I have not
tested the code with IE. But from C# language's perspective, it should
work. I hope this helps.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #9
Thanks. That worked in a Windows application. However, I can't get it to
work inside a Windows service, which is what I want. I get the following
exception when I run the line "shellWindows = new
SHDocVw.ShellWindowsClass();":

COM object with CLSID {9BA05972-F6A8-11CF-A442-00A0C90A8F39} is either not
valid or not registered.

I know that this CLSID belongs to ShellWindows.

Any ideas?

Thanks again.
"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:Uw**************@cpmsftngxa08.phx.gbl...
Hello,

Thanks for your update. Let's try the following:

ClassA:
public static SHDocVw.ShellWindows shellWindows = new
SHDocVw.ShellWindowsClass();

public static void ie_BeforeNavigate2(object disp, ref object url, ref
object flags, ref object targetFrameName, ref object postData, ref object
headers, ref bool cancel)
{
MessageBox.Show("BeforeNavigate2: " + url.ToString());
}

ClassB:

in the constructor

foreach(SHDocVw.InternetExplorer ie in ClassA.shellWindows)
ie.BeforeNavigate2 += new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(ClassA.ie_BeforeNavig ate2);

ClassC:

in the constructor

foreach(SHDocVw.InternetExplorer ie in ClassA.shellWindows)
ie.BeforeNavigate2 -= new
SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHan dler(ClassA.ie_BeforeNavig ate2);

Since the "shellWindows" is a static member, we can access it from both
ClassB and ClassC. We only need to "new" it once in ClassA. I have not
tested the code with IE. But from C# language's perspective, it should
work. I hope this helps.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #10
excelent.
regards.
"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:sT*************@cpmsftngxa07.phx.gbl...
Hi,

Thanks for posting. The following code is for your reference:

using System;

public delegate void ADelegate();

public class A
{
public static void Main(string[] args)
{
B b = new B();
ADelegate d = new ADelegate(AMethod);
b.AEvent += d;
C c = new C();
c.RemoveDelegateFromB(d, b);
}

public static void AMethod()
{
Console.WriteLine("A method");
}
}

public class B
{
public event ADelegate AEvent;
public void RaiseAEvent()
{
if (AEvent != null)
{
AEvent();
}
}
}

public class C
{
public void RemoveDelegateFromB(ADelegate d, B b)
{
b.AEvent -= d;
}
}

I hope this helps. If there is anything else I can help with, please feel
free to post here.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #11

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

Similar topics

18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
4
by: Claudio Jolowicz | last post by:
I am trying to find a solution to the following design problem (code at the bottom): We are implementing a trader agent that can trade with other traders on an electronical trading platform. To...
3
by: R Millman | last post by:
under ASP.NET, single stepping in debug mode appears not to stop within event procedures. i.e. 1) Create web page with submit button and event procedure for the click event in the code behind...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
5
by: james | last post by:
Hello, I am having a little trouble creating an event handler for a context menu toolstripmenuitem. I've seen various tutorials and so on, but I keep getting a bit stuck! So far I have a second...
9
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the...
8
by: hoofbeats95 | last post by:
I don't think this should be this complicated, but I can't figure it out. I've worked with C# for several years now, but in a web environment, not with windows form. I have a form with a query...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.