How to set focus on a control on a form that has been opened through code?

The other day, a question came up if we can open a form through code and focus on a particular control?

Well, we surely can easily do that.

The setFocus() method is commonly used to put the focus on any control.

The question was to put focus on a control on a form which was open through code.

The solution is to first get a reference of the control on which you want to set the focus and then call setFocus on it.

The FormDesign class has a method controlName() which will return us the control. It takes a string argument.

The below code shows us how to do what we just discussed.

static void setFocus(Args _args)
{
Args args;
FormRun formRun;
FormControl formControl;
;

args = new Args(formStr(Address));
formRun = classFactory.formRunClass(args);

formRun.run();
formRun.detach();

// Get a reference of the control we want to set focus on
formControl = formRun.design().controlName(identifierStr(Description_Name));
formControl.setFocus();
}

Hope this helps.

Comments

Popular posts from this blog

How to add empty ranges in query

The field with ID '0' does not exist in table - Cause and resolution.

Get selected records in Dynamics AX 2012