DoubleEditBox - Allows entering more digits than format

Editors for WPF Forum

Posted 8 months ago by Brad Salmon
Version: 22.1.0
Avatar

I set the Format to "F3". The control allows entering more than 3 digits after the decimal. When I tab out, the control then rounds to display only 3 digits. 

Is there any way to have the control only allow a user to enter 3 digits after the decimal? I don't a user to enter data accidentally and get an incorrect value (i.e., if they meant to enter 1.951 but accidentally entered 1.9519 the value will end up being 1.952)

Comments (1)

Answer - Posted 8 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Brad,

Unfortunately there isn't a built-in way to prevent certain typing of text since numbers can be specified in multiple ways, basically any number-related text that can be parsed from a string in the current culture.  For doubles, that even includes E-notation, etc.

The edit box has a TextBox in its template that is where you type into.  We watch for changes in that and the convert its Text to a double value in the virtual TryConvertFromString method.

You could try something like this in a TryConvertFromString override:

protected internal override bool TryConvertFromString(string textToConvert, bool canCoerce, out Double? value) {
	var success = base.TryConvertFromString(textToConvert, canCoerce, out value);
	if ((success) && (value == 1.9519) && (ActiproSoftware.Windows.Media.VisualTreeHelperExtended.GetFirstDescendant(this, typeof(TextBox)) is TextBox textBox)) {
		textBox.Text = "1.951";
		textBox.SelectionStart = textBox.Text.Length;
		return false;
	}

	return success;
}

Obviously that is a specific test for your scenario and you'd want to rework it to be more general.


Actipro Software Support

The latest build of this product (v22.1.3) was released 21 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.