Setting multi-line text box control in a Microsoft Dynamics GP add-in to be empty or blank
When working with multi-line text boxes (Text Tool), rather than single line edit boxes, it is easy to introduce a bug to your application.
The text box contents can be set with the the example code below:
ItemMaint.LocalTxtExtraInfo.Value = result.ExtraInfoDescription
…where the value of the right hand side is written to the control’s value and is thus displayed.
As the record set is scrolled, by for example, the user using the form’s scroll buttons, then this code may be called repeatedly to show this field’s value for each record in turn.
The problem I experienced, occurs when after displaying a value for the first record in the field, a subsequent record happens to be empty. In this case the empty string is assigned to the control’s value, but the control does not blank itself in the user interface. In fact in debug, looking at the control’s value immediately after setting it to string.empty, it is found that the value has not changed from the previous record.
So we have found that the field is not updated if an empty string is assigned to it. This seems to be a design decision in the way the objects have been designed to behave. Hence the field will retain the previous record’s value until the next non-blank value in encountered in the record set.
This not only misleads the user as the incorrect field data is displayed for what should be an empty field, but it also, if the record is “saveable” of writing that wrong value back to the database, thus corrupting the field’s value “permanently” in the database (this is what brought my attention to the issue).
So if this control is used in your addin- always remember to use the following pattern, or equivalent to check for empty values. This uses the fields clear method to correctly empty the field on empty strings.
If result.ExtraInfoDescription.Length = 0 Then
ItemMaint.LocalTxtExtraInfo.Clear()
Else
ItemMaint.LocalTxtExtraInfo.Value = result.ExtraInfoDescription
End If
'''
**Note:**
This behaviour is not followed by the normal edit boxes, they will blank when string.empty is assigned to them.
If you found this helpful, feel free to comment, it helps motivate me to write more!