Handling events of runtime form controls

Sometimes you may have to create a form during run time. That can be easily done by creating an object of the Form class. You can also create objects of different controls and add. But what if you want to handle events of these controls?

For this you have to tell the form system where to look for events. This can be done by calling the controlMethodOverload on the formRun with a true value. This makes the form look for methods on the formRun object. But we cannot add methods on the formRun object because it is created during runtime. So you can tell the form which object to look for the methods by providing the object to the controlMethodOverloadObject method.

Method’s name must have the following naming convention and it must be strictly followed, _ ex, MyButton_clicked

Create a class like given below,


class RunTimeFormClass
{
}


Add a method createForm to it like this


void createForm()
{
FormBuildDesign formBuildDesign;
FormButtonControl myButton;
FormRun formRun;
Form form;
Args args;
;
form=new Form();

formBuildDesign=new FormBuildDesign();
formBuildDesign=form.addDesign("Design");

myButton=formBuildDesign.addControl(FormControlType::Button,"myButton");
myButton.text("Click Me!");

args=new Args();
args.object(form);

formRun=new FormRun(args);
formRun.controlMethodOverload(true);
formRun.controlMethodOverloadObject(this);
formRun.init();
formRun.run();
formRun.detach();
}


Create another method myButton_clicked like this,


static void myButton_clicked()
{
;
Box::info("Button Clicked");
}


Now create a job and insert the following code,


static void createForm(Args _args)
{
RunTimeFormClass obj=new RunTimeFormClass();
obj.createForm();
}


That’s all, when you run the job you can see a form with a button, click the button and you will get a Box with “Button Clicked” message.

Comments

Popular posts from this blog

How to add empty ranges in query

Get selected records in Dynamics AX 2012

How to refer to form controls in code without setting the AutoDeclaration property in Dynamics AX?