This project has moved. For the latest updates, please go here.

IntegerUpDown Control - Only Numeric

Apr 27, 2012 at 7:53 AM

Hello everybody,

is it possible to allow only the chars between 0-9, backspace and enter?

It should do nothing if i type letters

I tried a lot of examples, no one is working

 

best regards

Hansoloko

 

Apr 27, 2012 at 8:58 AM

I think you could use the PreviewTextInput event to allow only integers (I think backspace and enter will be accepted but I'm not 100% sure).

An event handler like this should do the trick, just set the PreviewTextInput="DecimalUpDown_PreviewTextInput" on the IntegerUpDown control and the following function in the code:

private void DecimalUpDown_PreviewTextInput(object sender, TextCompositionEventArgs e) {
	var dud = (sender as Microsoft.Windows.Controls.DecimalUpDown);
	// deny entry of non digits
	int tmp;
	if (!int.TryParse(e.Text, out tmp))
		e.Handled = true;
}

 

HTH