DoubleEditBox - Allows entering more digits than format

Editors for WPF Forum

Posted 2 years 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 2 years 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 (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.