Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
834 views
in Technique[技术] by (71.8m points)

dynamic - AX2009 Loop through all the controls in the form on init

How is it possible to loop through all the controls in a form when a form is initialized?

I have tried the following in the init and run methods, but it does not work:

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

    switch ( control.handle() )
    {
        case classnum(FormBuildButtonControl):
            element.initButton(control);
        break;
        case classnum(FormBuildStaticTextControl):
            element.initStaticText(control);
        break;
    }
}

Is there any way of doing this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...