Calling Parent Form Public method in child Form

This question has suggested answer(s)

I have scenario I have a parent form which has a method M. If user clicks a button on Parent it opens a child form.In the child form if the user click 'ok' button i need to call a public method in parent form.Ppl around here are suggesting to use form run object which didnt wrk

i used two appoaches

1. {

object formrunobj;

formrunobj=args.caller();

formrunobj.M(); 

}

this approach didn't work.

2.I tried  to pass a string value args.parm() to parent form from child and basing on the string I am tried to run the method with if condition .It didn't work.

Please suggest me any ideas.If possible code example .Thanks in Advance.

All Replies
  • "Didn't work" means "I do it wrong", but you didn't give us enough information about the problem. What exactly happened?

    If the method can't be invoked, check how you get the args variable. Then verify that args.caller() returns the right object. Then ensure yourself that M() exists and has no parameters.

    You may also look at Dynamic method dispatch in X++.

    Martin "Goshoom" Dráb | Freelancer | Goshoom.NET Dev Blog

  • Could you please try the below code

    In the Parent form

    void parentMethod()

    {

       //some code here

    }

    In the child form, you can do this

    void callParentMethod()

    {

       FormRun callerForm = element.args().caller();

       if (callerForm)

       {

           if (callerForm.name() == formstr(<form name>))

           {

               callerForm.parentMethod();

           }

       }

    }

    Regards,

    Siva

  • In addition to what siva says, Please make sure if it is a command button or menu item button that invokes the child form. If it is a command button you may want to construct the args object through code by overriding the clicked method of the command button.

    - Kamal

     MyBlog | Twitter | LinkedIn

  • Yes ,Its a command button and I overrode clicked method ,and in the method I need to call a public method in the  parent form.But,when I am trying to use args.caller(). I am seeing the caller class instead of the parent form.So,I am unable to access the public method in parent form from child'd override method.I am wondering if there is an alternate approach to call parent forms public method.

  • I apologize for my unclear question....

  • Try the below line of code.It should work as you expected.

    callerForm.owner().parentMethod();

  • If the caller class has a reference to the parent form, simply call a method in the class from your child form and let the class to pass the message to the form.

    If you don't have any reference to the parent form, you simply can't call its methods - you have to change the implementation first to get the reference.

    Martin "Goshoom" Dráb | Freelancer | Goshoom.NET Dev Blog