2 Replies Latest reply on Apr 17, 2006 8:34 PM by agorman

    Child form calling a method on it's parent's class.

    agorman
      I am developing a form-based application. There is one root form called "Application" and a few child forms off the root (e.g., Login, CustView). I would like the sibling forms to be able to pass contol to each other (e.g., after successful login, I want to make Login invisible and CustView visible).

      I wanted the root form to control this interaction, but I'm not sure how events in child can be handled in the parent. This is a common technique, but in this case, the parent doesn't explicitly instantiate the child, so I can't just pass a reference to the parent to the child's constructor. I can use this.parentForm to get a reference to the parent form, but I can't seem to find the parent's methods, just it's gui elements (i.e., this.parentForm.parentMethod() doesn't work).

      Can some tell me how to do this? I am considering a FormManager class, which is a singleton class that each form can registers with and can use to toggle the visibility of itself and its siblings, but I wanted to see if there was an easier way to access methods on the class instance associated a parent's form.

      Any suggestions?

      Andy
        • 1. Re: Child form calling a method on it's parent's class.
          Level 7

          "agorman" <webforumsuser@macromedia.com> wrote in message
          news:e2163h$4na$1@forums.macromedia.com...
          >I am developing a form-based application. There is one root form called
          > "Application" and a few child forms off the root (e.g., Login, CustView).
          > I
          > would like the sibling forms to be able to pass contol to each other
          > (e.g.,
          > after successful login, I want to make Login invisible and CustView
          > visible).
          >
          > I wanted the root form to control this interaction, but I'm not sure how
          > events in child can be handled in the parent. This is a common technique,
          > but
          > in this case, the parent doesn't explicitly instantiate the child, so I
          > can't
          > just pass a reference to the parent to the child's constructor. I can use
          > this.parentForm to get a reference to the parent form, but I can't seem to
          > find
          > the parent's methods, just it's gui elements (i.e.,
          > this.parentForm.parentMethod() doesn't work).
          >
          > Can some tell me how to do this? I am considering a FormManager class,
          > which
          > is a singleton class that each form can registers with and can use to
          > toggle
          > the visibility of itself and its siblings, but I wanted to see if there
          > was an
          > easier way to access methods on the class instance associated a parent's
          > form.
          >
          > Any suggestions?
          >

          Why don't you explicitly instantiate the children with a reference to the
          parent form. That way you could store the reference in the constructor. As
          long at the functions are public I think it should work.

          Amy


          • 2. Re: Child form calling a method on it's parent's class.
            agorman Level 1
            Thanks for the reply. My problem may have been a typo, but exploring your suggestion raised a question in my mind.

            The child form get instantiated in my code by the parent. It also gets automatically instantiated when the application loads. I guess this is because the form is crewated in the swf file and not dynamically, in the code. I am curious about the life cycle of an aplication and its forms. Can I create a form in the Flash authoring environment and not instantiate it automatically (I know there's an autoload property, but I don't think that's the answer. I haven't seen any detailed documentation on this. Any suggestions?

            The following code produces the subsequent output.

            class org.clever.caregiverconnect.Application extends mx.screens.Form {
            public var caregiverLogin_frm:CaregiverLogin;
            function Application() {
            trace("constructing Application: " + this);
            this.caregiverLogin_frm = new CaregiverLogin(this);
            }
            }


            class org.clever.caregiverconnect.CaregiverLogin extends mx.screens.Form {
            private var parentFormInstance:Application;
            public function CaregiverLogin (parentFormInstance:Application) {
            super();
            this.parentFormInstance = parentFormInstance;
            trace("constructing CaregiverLogin parentFormInstance: " + this.parentFormInstance);
            trace(" parentForm : " + this.parentForm);
            trace(" ");
            }
            }

            Output Trace
            constructing Application: _level0.application_frm
            constructing CaregiverLogin parentFormInstance: _level0.application_frm
            parentForm : null

            constructing CaregiverLogin parentFormInstance: undefined
            parentForm : _level0.application_frm