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?
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.
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'.
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?
Re: Raise events in places
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.