|
-
May 10th, 2006, 06:53 PM
#1
Thread Starter
PowerPoster
Raise events in places
Hi.
hmm trying to grasp this once again.
I have:
MainForm
subclasses
I want the mainform to raise an event so the subclasses can "hear it" and act upon it.
I know how to do this for the Mainform, so when an event is raised in the subclasses, the UI gets called and acts upon it. No doubt it shouldnt be different and I've tried to "reverse" the way so that the subclasses can hear the event but not going to plan.
This is what I have:
MainForm:
Code:
public delegate void SendNewNotification(MyClass notification)
public event SendNewNotification OnEventSendNewNotification;
subclass:
Code:
public delegate void SendNewNotification(MyClass notification)
public event SendNewNotification OnEventSendNewNotification;
MainForm.SendNewNotification theNewNotification;
..
..
..
this.OnEventSendNewNotification += new SendNewNotification(Myclass2_OnEventSendNewNotification) //it also inserts this event method in this class for me to do my own thing
this.theNewNotification = new MainForm.SendNewNotification(OnEventSendNewNotification);
When time comes for the UI to raise this event, how do I do it? Everytime I try to do it "if (this.OnEventSendNewNotification != null){}" it always tells me it is null therefore it wont raise the event.
Last edited by Techno; May 10th, 2006 at 10:35 PM.
-
May 10th, 2006, 08:46 PM
#2
Re: Raise events in places
What do you mean by subclasses? Do you mean classes that inherit MainForm or are you talking about other forms that MainForm creates at run time?
-
May 10th, 2006, 09:03 PM
#3
Thread Starter
PowerPoster
Re: Raise events in places
just classes .cs files, standard classes, no ui. just object classes which the main form creates at runtime after a user selects a command to do some action, such as communicate over a network.
-
May 10th, 2006, 09:14 PM
#4
Re: Raise events in places
Subclass is C-speak for a class that derives from a base, so your initial terminology implied that MainForm was the base class and these other objects were instances of classes derived from MainForm.
The whole point of events is that the object raising the event doesn't have to know anything about the objects handling it. The objects handling the event have to know about the object raising it though. Let's say it's dinner time in the household and the cook yells out "dinner's ready". The cook doesn't have to know who's in the house or even if anyone is there. They've raise the DinnerIsReady event and it's up to those listening to decide how to handle that event.
For you child objects to be able to handle an event raised by your main form they are going to have to have a reference to the form. In your code you have added an event handler to the 'this.OnEventSendNewNotification', so you're attempting to handle an event of the current object. That's not what you want though. You want to handle an event of the main form, so you need a reference to the main form to put in place of 'this'.
-
May 10th, 2006, 09:31 PM
#5
Thread Starter
PowerPoster
Re: Raise events in places
Thanks for that, although still a tad confused 
"this" is not in the mainform but in "MyClass"
The way the code is right now is what I have posted up here.
I just dont know how to start to raise the event on the main form so when I raise this event, the MyClass hears it and acts.
I have created an eventhandler in MyClass, and have "wired" up MainForm.OnEventSendNewNoification in MyClass but also having the delegate and event put in the MainForm
When MyClass is constructed, it creates the event handler in MyClass. Cool.
but now I want the mainform to raise this event, how?
-
May 10th, 2006, 10:39 PM
#6
Thread Starter
PowerPoster
Re: Raise events in places
Think i got it
Last edited by Techno; May 10th, 2006 at 11:00 PM.
-
May 16th, 2006, 07:19 PM
#7
Thread Starter
PowerPoster
Re: Raise events in places
Bit of a problem just had a break of a few days and trying to get into the grip of this.
from my classes (not UI MainForm), It needs to raise an event to the UI.
How can I do this? Everytime I look at it, I get the problem again where the OnEvent is null.
I guess its because I did not "wire up" the OnEventname from the UI to this class. But surely the UI should not need to know about this class, as its no relevence to the UI - this class is an outsider to the UI, the class handles communication via TCP.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|