AX2009 Loop through all the controls in the form on init

。_饼干妹妹 提交于 2019-11-28 14:02:56

The code you have works, but it only iterates through the top level of the design. You need to build a recursive function to iterate through the entire set of controls, and place it in the init method before the super():

void init()
{
    int     i;
    void    processControls(FormBuildControl fbc)
    {
        int     j;
        ;

        if (fbc.isContainer())
        {
            for (j = 1; j < fbc.controlCount(); j++)
            {
                //Process container, if you need to
                processControls(fbc.controlNum(j)); 
            }
        }
        else
        {
            //Control is not a container, process it here.                
        }
    }
    ;

    for (i = 1; i <= element.form().design().controlCount(); i++)
    {
        processControls(element.form().design().controlNum(i);
    }

    super();
}

The super() call will automatically initialize all the controls on the form. Once they are initialized, you won't be able to use FormBuildControl type objects to configure the fields, as they will already be consumed onto the form. If you need to modify the field after initialization, you should refer to the field directly (though I'm unsure of how you could get the field name and reference it via X++).

Instead of conditionally initializing the controls, call super() and simply hide the field conditionally, or use security to hide information you don't want certain people to have access to.

EDIT: Since you are dealing with FormBuildControls, which are pre-initialized designs, you should have the super() call after the initial processControls call. I've changed my example to reflect this.

Each control can have child controls and parent controls. You're only going over the first level. Here is a sample job that demonstrates how to use recursion over form controls.

static void recurseOverAllFormControls(Args _args)
{
    Form form = new Form(formstr(SalesTable));

    void recurse(Object _parent, int _depth = 1)
    {
        int     i;
        str     name;
        str     caption;
        str     dashes;
        ;

        // Used for making it pretty
        //-->
        i = _depth;
        while (i)
        {
            dashes += '-';
            i--;
        }
        //<--

        // Used for example of how to use data
        //-->
        if (SysTest::hasMethod(_parent, identifierStr(caption)))
            caption = _parent.caption();

        if (SysTest::hasMethod(_parent, identifierStr(name)))
            name = _parent.name();

        info(strfmt("%1%2[%3](%4)", _depth, dashes, name, caption));
        //<--

        // Escape condition!
        if (_parent.controlCount() == 0)
            return;

        // Recursive statement
        for (i=1; i<=_parent.controlCount(); i++)
            recurse(_parent.controlNum(i), _depth+1);
    }
    ;

    recurse(form.design());
}

Edit: Ah somebody beat me to the answer. Hopefully somebody will appreciate my demo job.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!