Completed
Last Updated: 10 Oct 2018 10:40 by ADMIN
ADMIN
Martin Ivanov
Created on: 05 Jan 2018 15:07
Category: MaskedInput
Type: Bug Report
2
MaskedInput: The milliseconds are not parsed properly when the Mask property allows them
For example, the Mask "hh:mm:ss.ff" will render the value properly. But if you change the value at runtime (by pressing a key on the keyboard), the milliseconds will be re-parsed and will be displayed wrongly. 

For example, if you have the value "12:13:14.50" and you go to the ".50" part (between the decimal separator and the number 5). And then press 3, the milliseconds will change to ".03".

To work this around you can create a custom masked input control and override its CoerceTextOverride() method. There you can implement some logic that replaces the wrong milliseconds text. Here is an example in code:

public class CustomMaskedDateTimeInput : RadMaskedDateTimeInput
{
	protected override string CoerceTextOverride(ref int selectionStart)
	{
		var text = base.CoerceTextOverride(ref selectionStart);
		if (this.Value.HasValue && 
			this.Mask != null && 
			this.Mask.Contains("hh:mm:ss.f"))
		{
			
			int millisecondsAllowedLength = GetAllowedMillisecondsLength(this.Mask); // example: .ff means that there are 2 allowed millisecond chars
			int millisecondsMaxLength = 3; // max = 999

			var milliseconds = this.Value.Value.Millisecond.ToString(); // the original milliseconds give from the value
			var zeroesCount = millisecondsMaxLength - milliseconds.Length; // how many zeroes should be append before the millseconds string value

			milliseconds = new string('0', zeroesCount) + milliseconds; // append the zeroes

			// apply the milliseconds restriction given by the Mask (.ff - two symbols)
			if (milliseconds.Length > millisecondsAllowedLength)
			{
				int charsToRemoveCount = milliseconds.Length - millisecondsAllowedLength;
				for (int i = 0; i < charsToRemoveCount; i++)
				{
					milliseconds = milliseconds.Remove(milliseconds.Length - 1, 1);
				}
			}                

			// replace the default parsed millseconds part of the string with the custom milliseconds
			var millisecondsIndex = text.LastIndexOf(".") + 1;
			text = text.Remove(millisecondsIndex, millisecondsMaxLength - millisecondsAllowedLength);
			text = text.Insert(millisecondsIndex, milliseconds);
		}

		return text;
	}

	private int GetAllowedMillisecondsLength(string mask)
	{
		int length = 0;
		var millisecondsIndex = mask.LastIndexOf(".") + 1;
		if (millisecondsIndex != -1)
		{
			length = mask.Length - millisecondsIndex;
		}            
		return length;
	}
}
1 comment
ADMIN
Ralitsa
Posted on: 10 Oct 2018 10:40
Hi,

The fix will be available in our next official version – R3 2018 SP1, scheduled for 17-th October 2018 (Wednesday).

Best regards,
Ralitsa Kumanova