Search code examples
x++dynamics-ax-2012

How can a string control be added in a form during run time?


I want to add a string control on a form during run time when a button is clicked.

What I have tried so far:

  1. Created a form
  2. Added run form method
  3. Added runTimeControl_validate form method
  4. Added button on the form

The button has the following code in its clicked method:

void clicked()
{
    FormBuildDesign       design = Form.design();
    FormBuildGroupControl formBuildGroupControl;
    FormStringControl     c;
    FormControlType       fC;        
    ;

    // c = addGroup.addControl(FormControlType::String, 'RunTimeControl');

    c = ButtonGroup.addControl(fC::String, 'test');
    c.label("New control");
    formBuildGroupControl = formBuildDesign.control(addGroup.id());
}

I am getting error in the line c = ButtonGroup.addControl(fC::String, 'test');

Error: Enumeration doesn't exist


Solution

  • Firstly, replace fC::String with FormControlType::String.

    Secondly, string controls cannot be added to button groups (ButtonGroup control type) - add it to a normal Group instead.

    Thirdly, to avoid such issues as missing labels, etc., it makes sense to add element.lock(); before adding the control and element.unlock(); after updating its label. - ignore this.