Condividi tramite


begin

private void TextBox_OnKeyDown(object sender, KeyRoutedEventArgs e)

{

// Supports numbers and a single decimal

// By default this should be the "." for US English, but lets get the system assigned value just in case

String decimalString = System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator;

char decimalChar = Convert.ToChar(decimalString);

char theKey = Convert.ToChar(e.Key);

int thePeriodKey = 190;

TextBox tb = (TextBox)sender;

if (Char.IsDigit(theKey) || Char.IsControl(theKey)) { } // it's a digit

else if (theKey == thePeriodKey && tb.Text.IndexOf(decimalString) == -1) { } // is it the first '.'

} else // not a valid key

{

e.Handled = true; // we have to indicate we handled it or the key will be put in the control

PlayErrorSound();

var specialFX = msgTextBlock.Resources["errorMessageAnimation"] as Storyboard;

specialFX.Begin();

}