A blog offering various X++ code snippets and tips for Microsoft Dynamics AX

Tuesday, July 29, 2008

Traversing run time controls

Welcome to another AX development lesson.

Today i will discuss traversing controls created at run time to get/set their properties or use them in your code.

First, you have to get a reference to the formDesign of your form. This can be done by calling element.design() in your form. don't forget that you can pass this to as a paramter to a method, so you will be able to process the controls in a seperate class. You method has to accapet a formDesign variable as a parameter as shown in the following code:


public void controlsProcessor(FormDesign _formDesign)
{
  FormBuildControl formBuildControl;
  Counter counter;
  FormCheckboxControl formCheckboxControl
  ;

  for (counter = 1; counter <= formDesign.controlCount(); counter++)
  {
    // If current value of counter represent a valid control id in the form
    formBuildControl = formDesign.controlNum(counter);

    // If the current looped control is a textbox
    if (SysFormRun::controlType(classidget(formBuildControl)) == FormcontrolType::CheckBox)
    {
      // Assign it to a formCheckboxControl
      // to get access to properites and methods
      // usually found on checkboxes
      formCheckboxControl = formDesign.controlNum(counter);

      // If the checkbox is checked
      if (formCheckboxControl.value())
      {
      // Do your custom processing
      }
    }
  }
}


Obviously you can use any type of form controls not just checkboxes by using the corresponding control classes.

You could call the above method from a form by using the following line of code:


  myClassInstance.controlsProcessor(element.design);


If you any questions or comments e-mail me on: mirko@mirkobonello.com

No comments: