/ Dynamics

# [x++] Change labels of form controls on runtime

Sometimes the customer wants a label to be replaced only on a specific form. A good example is the field ItemId (Item number). Normally you would change the label on the underlaying extended data type but in this case this would change the label systemwide and this is by far not our intention. We just want to change it on one form.

My current example is the field inventTransRefId on the form WMSOrder. The standard label is "Number" which we agree is not really self explaining.
So to replace the label only on the one form, we habe to modify the init method on the form.

###### init()

First add the call to our new function before the super() call in the forms init-method:

void init()
{
// ...
element.setControls(); // important to call this before super!
super();
// ...
}
###### setControls()

Next create a new method for iterating all the controls on the form:

public void setControls(FormBuildControl _control = null)
{
int                 ctrlCount;
int                 ctrlIdx;
FormBuildControl    fbc;

if (_control)
{
ctrlCount = _control.controlCount();
for (ctrlIdx = 1; ctrlIdx <= ctrlCount; ctrlIdx++)
{
fbc = _control.controlNum(ctrlIdx);
element.setLabel(fbc);
if (fbc.isContainer() && fbc.controlCount())
{
element.setControls(fbc);
}
}
}
else
{
ctrlCount = element.form().design().controlCount();
for (ctrlIdx = 1;ctrlIdx <= ctrlCount;ctrlIdx++)
{
fbc = element.form().design().controlNum(ctrlIdx);
element.setLabel(fbc);
if (fbc.isContainer() && fbc.controlCount())
{
element.setControls(fbc);
}
}
}
}
###### setLabel()

The last method is the method to actually check the control and if it's our wanted control then change the label:

public void setLabel(FormBuildControl  _control)
{
LabelId                 newLabelIdOrderNum = literalStr("@SYS15098");
FormBuildStringControl  fbsc;
if (_control is FormBuildStringControl)
{
fbsc = _control;
if (fbsc.dataField() == fieldId2Ext(fieldNum(WMSOrder, inventTransRefId), 1))
{
fbsc.label(newLabelIdOrderNum);
}
}
}
###### Taking it further

If you have to change many labels on the form, you should rediscuss this with your customer again... ;) No seriously you can then use a Map with FieldId2Ext mapped to the new label and use the map to decide weather to change a label on a control or not and get the label from the map.

Map	fieldLabelMap = new Map(Types::Int64, Types::String);
if (fieldLabelMap.exists(fbsc.datafield()))
{
fbsc.label(fieldLabelMap.lookup(fbsc.datafield()));
}

#### Robin

Software Engineer / Application Developer

[x++] Change labels of form controls on runtime