473,722 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# events - 2 independent classes listening to each other?

hi guys, I have a query about c# event handling that should be obvious
I am sure - but I am confused :-)

Lets say I have two classes.

One is a user control with a tabcontrol on it
The other is a user control with a treeview on it.

Now, both of these controls are placed on a form. Both classes raise
events e.g. when the tab is changed on the first control, the tree
control wants to know about it and vice versca.

so far so good...

Now, given that these controls know NOTHING about each other, how on
earth do I register each as a listener of the other? Neither has a
reference to the other one.

Since they are both placed on the containing form I am assuming that
this containing form must act as an event proxy - i.e. it will listen
for all events raised and call the relevant public handler in each user
control embedded on it (since it has references to all of them).

Am I talking rubbish or is there a better way? In all the examples I
see its one way only, the subscriber MUST know about the event raising
class but my situation is that they both must subscribe to each other.

thanks for reading - having a bad code day :-)

Nov 17 '05 #1
6 3142
Gavin,

You could have the control listen to events on the other control, but
the limitation here is that there are no methods exposed on these controls
which will fit the event handler signature (you can't just assign methods to
call arbitrarily).

So, like you said, you will have to listen for relevant events, and then
make the call on the appropriate controls when those events are fired, just
like you would anything else.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<ga***@my-deja.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
hi guys, I have a query about c# event handling that should be obvious
I am sure - but I am confused :-)

Lets say I have two classes.

One is a user control with a tabcontrol on it
The other is a user control with a treeview on it.

Now, both of these controls are placed on a form. Both classes raise
events e.g. when the tab is changed on the first control, the tree
control wants to know about it and vice versca.

so far so good...

Now, given that these controls know NOTHING about each other, how on
earth do I register each as a listener of the other? Neither has a
reference to the other one.

Since they are both placed on the containing form I am assuming that
this containing form must act as an event proxy - i.e. it will listen
for all events raised and call the relevant public handler in each user
control embedded on it (since it has references to all of them).

Am I talking rubbish or is there a better way? In all the examples I
see its one way only, the subscriber MUST know about the event raising
class but my situation is that they both must subscribe to each other.

thanks for reading - having a bad code day :-)

Nov 17 '05 #2
What you are referring to is the Observer pattern. Microsoft has a
article that explains the pattern and how to do this with events and
delegates.
http://msdn.microsoft.com/library/de...verpattern.asp

You are essentially correct, in that the they must be wired up in the
aspx page. This article takes it a step further and puts a method in
the "Observer object" that acts as the delegate that "listens" for the
event in your second object.

Nov 17 '05 #3
hi! thanks for the help guys - I should have mentioned that its a c#
windows application rather than as ASPX, I assume the observer pattern
as you describe above still holds?

thanks!

Nov 17 '05 #4
john_teague wrote:
What you are referring to is the Observer pattern. Microsoft has a
article that explains the pattern and how to do this with events and
delegates.
http://msdn.microsoft.com/library/de...verpattern.asp

You are essentially correct, in that the they must be wired up in the
aspx page. This article takes it a step further and puts a method in
the "Observer object" that acts as the delegate that "listens" for the
event in your second object.


I'm sorry, but I don't think this is the right answer. Of course, an
object listening to events from another object is an example of the
observer pattern, but the specific problem here is that the objects don't
have any reference to each other.

I believe a much better pattern to suggest as a solution to the specific
problem is the Mediator pattern. There's a good description with a quick
introduction at the following URL:
http://my.execpc.com/~gopalan/design.../mediator.html

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #5

Oliver Sturm wrote:
john_teague wrote:
What you are referring to is the Observer pattern. Microsoft has a
article that explains the pattern and how to do this with events and
delegates.
http://msdn.microsoft.com/library/de...verpattern.asp

You are essentially correct, in that the they must be wired up in the
aspx page. This article takes it a step further and puts a method in
the "Observer object" that acts as the delegate that "listens" for the
event in your second object.


I'm sorry, but I don't think this is the right answer. Of course, an
object listening to events from another object is an example of the
observer pattern, but the specific problem here is that the objects don't
have any reference to each other.

I believe a much better pattern to suggest as a solution to the specific
problem is the Mediator pattern. There's a good description with a quick
introduction at the following URL:
http://my.execpc.com/~gopalan/design.../mediator.html

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Hi again, I just read about the mediator pattern and it does seem to
work well for my needs, I rattled yup a quick prototype and am quite
happy with how it hangs together.

I do appreciates everyones help on this, thanks!

Nov 17 '05 #6
I think you're right. I haven't seen the mediator before, but looks
much better in this case. Sorry, I'm a web developer, so I hear
controls and assume web controls.

I'd like to see your proto type.

Nov 17 '05 #7

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

Similar topics

2
2020
by: Gizmo | last post by:
hi all i have a bit of a prob with some 2 classes iv created if say i have class A and class B. class A hold an atribute of class B and class be Holds an Atribute of class A. both are declared as pointers in each others class and iv used #ifndef in both the .h's of each class.
6
9942
by: Cockroach | last post by:
Hello, I have a problem where the onClick of a table row will activates a window.location event, and inside a cell in that row, an image onClick event shows/hides a div. The problem is that when you click on the image, it briefly shows the div, and then reloads the page to the window.location url. Is there a way of preventing the onClick of the row (<tr>) from doing
7
4911
by: Manuel Bleichner | last post by:
Hello list, I have searched for some time now, but no result... I'm having the following problem: In a module I have a huge number of classes of the form: class A(object): connected_to = <other attributes...>
8
2818
by: Eric | last post by:
I am writing a program with several header files. I would like for some of them to be aware of others. Here is a small example of my problem in three different files: //// main.c++ #include "A.h" #include "B.h" int main () { using namespace std;
2
1886
by: Lakenarr | last post by:
Hi, I need to have two objects referencing eash other. Something like this: class Planet { private Moon m_Moon; public Moon Moon {
5
1674
by: stefven blonqhern | last post by:
hi, i imagine we've all seen this one posted before but i can't get any solutions to work for me.. for example: i have two classes MyClass and Shape. MyClass creates Shapes (and derived classes from Shape) and it also wants to pass a pointer to itself to a member function of Shape so that it can access MyClass' members. Pseudo code example: class MyClass {
3
4031
by: Eps | last post by:
Hi, I am writing an mp3 player using wpf, I have three user controls (media library, media controls and a playlist). Obviously I need these to interact with each other, I assume the best way to do this is to use events right ?. Can I raise an event on one user control and have one of the others suscribe to it ? or do I need to have the main window suscribe to it and then pass the message on to the target user control ?.
1
1477
by: puzzlecracker | last post by:
Hello Group, Say I have classes A and B and the both need to maintain a pointer/ reference to each for various reasons, say optimization. And there are other classes in the project use these two classes, among others. What is the best design for such architecture? I saw errors by explicitly including headers of both classes. Also,
2
2671
by: mirketiger | last post by:
I have this code it is not important tah methods have Serbian name it;s not that that's problem :) class cosinus; class sinus : public funkcija { private: funkcija *_op;
0
9386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9239
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9158
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8059
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6685
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5996
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4764
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2606
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.