If you’re still reading, you’re probably a Dynamics SL programmer, scratching your head. I had a requirement today that I guess I’ve never had before: set the Mask property dynamically at runtime based on values in a definition table. The table contained the mask character and length.
Of course, you need to use the SL SDK method SetProps. I created the string with the new mask, in this case “UUUUU” (five characters long). The original mask had 15 characters. According to the documentation, that ought to work …you can decrease the mask length, but not increase it.
The instant I typed a character into the control at runtime, I received an ArgumentOutOfRangeException: “Index and count must refer to a location within the string.”
The resolution was to use the control’s MaskLength with PadRight to add spaces to the end of the mask to equal the length of the original mask. That resolved the error. Here’s the VB code (in my test case, maskChar was a ‘U’ and length=5):
Dim origMaskLength = DirectCast(control, DSLMaskedText).MaskLength
Dim newMask As New String(maskChar, length)
newMask = newMask.PadRight(origMaskLength)
SetProps(Me, control, control, PROP_MASK, newMask)