Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Problem with events...

Problem with events...

Scheduled Pinned Locked Moved C#
help
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kaikus
    wrote on last edited by
    #1

    I have a problem... public delegate MyDelegate() class MDIParent { public event MyDelegate myEvent; public MDIParent() //Constructor { // Create and open MDIChild from MDIParent Constructor MDIChild mdiForm = new (MDIChild()) } Class MDIChild { public MDIChild //Constructor { ((MDIParent)this.MdiParent).myEvent += new MyDelegate(myPriveteMethod); } } Well, as you can see, I - Auntomatcally create a MDIChild from MDIParent constructor - In the MDIChild constructor I try to suscribe to an MDIParent event It gives me a SystemNullException. It perfectly suscribes if I do it OUTSIDE the MDIChildForm constructor. I suppose it is becouse parent form is not still initialized (child is created form INSIDE parent's constructor). I need to open MDIChild from the begginig, and suscribe to event at the begginning, too. Any workaround to do this without using the constructor Thanks!

    P N H 3 Replies Last reply
    0
    • K Kaikus

      I have a problem... public delegate MyDelegate() class MDIParent { public event MyDelegate myEvent; public MDIParent() //Constructor { // Create and open MDIChild from MDIParent Constructor MDIChild mdiForm = new (MDIChild()) } Class MDIChild { public MDIChild //Constructor { ((MDIParent)this.MdiParent).myEvent += new MyDelegate(myPriveteMethod); } } Well, as you can see, I - Auntomatcally create a MDIChild from MDIParent constructor - In the MDIChild constructor I try to suscribe to an MDIParent event It gives me a SystemNullException. It perfectly suscribes if I do it OUTSIDE the MDIChildForm constructor. I suppose it is becouse parent form is not still initialized (child is created form INSIDE parent's constructor). I need to open MDIChild from the begginig, and suscribe to event at the begginning, too. Any workaround to do this without using the constructor Thanks!

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Kaikus wrote:

      Auntomatcally

      :-D You're defining extended relationships in your code. :-D

      1 Reply Last reply
      0
      • K Kaikus

        I have a problem... public delegate MyDelegate() class MDIParent { public event MyDelegate myEvent; public MDIParent() //Constructor { // Create and open MDIChild from MDIParent Constructor MDIChild mdiForm = new (MDIChild()) } Class MDIChild { public MDIChild //Constructor { ((MDIParent)this.MdiParent).myEvent += new MyDelegate(myPriveteMethod); } } Well, as you can see, I - Auntomatcally create a MDIChild from MDIParent constructor - In the MDIChild constructor I try to suscribe to an MDIParent event It gives me a SystemNullException. It perfectly suscribes if I do it OUTSIDE the MDIChildForm constructor. I suppose it is becouse parent form is not still initialized (child is created form INSIDE parent's constructor). I need to open MDIChild from the begginig, and suscribe to event at the begginning, too. Any workaround to do this without using the constructor Thanks!

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        How about passing MDIParent instance into MDIChild's constructor?

        class MDIParent
        {

        public event MyDelegate myEvent;
        public MDIParent() //Constructor
        {
        // Create and open MDIChild from MDIParent Constructor
        MDIChild mdiForm = new MDIChild(this);
        }

        Class MDIChild
        {
        public MDIChild(MDIParent parent) //Constructor
        {
        parent.myEvent += new MyDelegate(myPriveteMethod);
        }

        }

        Best wishes, Navaneeth

        1 Reply Last reply
        0
        • K Kaikus

          I have a problem... public delegate MyDelegate() class MDIParent { public event MyDelegate myEvent; public MDIParent() //Constructor { // Create and open MDIChild from MDIParent Constructor MDIChild mdiForm = new (MDIChild()) } Class MDIChild { public MDIChild //Constructor { ((MDIParent)this.MdiParent).myEvent += new MyDelegate(myPriveteMethod); } } Well, as you can see, I - Auntomatcally create a MDIChild from MDIParent constructor - In the MDIChild constructor I try to suscribe to an MDIParent event It gives me a SystemNullException. It perfectly suscribes if I do it OUTSIDE the MDIChildForm constructor. I suppose it is becouse parent form is not still initialized (child is created form INSIDE parent's constructor). I need to open MDIChild from the begginig, and suscribe to event at the begginning, too. Any workaround to do this without using the constructor Thanks!

          H Offline
          H Offline
          hammerstein05
          wrote on last edited by
          #4

          The issue is that the MdiParent is not set within the construction of your child window. It's a property you set after the MDIChild has been created. You could create a register method on your child class and after you've set the MDIParent of the class, call MDIChild.Register( ). Not the best way, I'm certain, however it would do what you needed.

          K 1 Reply Last reply
          0
          • H hammerstein05

            The issue is that the MdiParent is not set within the construction of your child window. It's a property you set after the MDIChild has been created. You could create a register method on your child class and after you've set the MDIParent of the class, call MDIChild.Register( ). Not the best way, I'm certain, however it would do what you needed.

            K Offline
            K Offline
            Kaikus
            wrote on last edited by
            #5

            Thanks! I knew that was the problem. Parent was not completely set becouse I tried to suscribe inside child constructor, wich was invoked from the parent constructor. I tried your suggestion. It worked! But, how, and when to suscribe? Today morning, after a good rest, everything came clear. I just moved the suscription to the Forn_Load event :) Thanks!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups