Results 1 to 7 of 7

Thread: Raise events in places

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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'.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Raise events in places

    Think i got it
    Last edited by Techno; May 10th, 2006 at 11:00 PM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width