views:

1536

answers:

3

I'm trying to see if the user has pressed a decimal separator in a text box, and either allow or suppress it depending on other parameters.

The NumberdecimalSeparator returns as 46, or '.' on my US system. Many other countries use ',' as the separator. The KeyDown event sets the KeyValue to 190 when I press the period.

Do I just continue to look for commas/periods, or is there a better way?

Thanks!

A: 

I don't know in C#, but I'm almost sure you can find some constant that represents the decimal separator and other international settings, so you can compare with that.

I wouldn't compare specifically with a manually defined constant for that kind of things. Sorry I can't give you the API, as I work with C++Builder, but I'm sure there should be some predefined way to get that values.

Rodrigo Gómez
+4  A: 

The call

CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator

gets the decimal separator for the current user interface culture. You can use other cultures to get the separator for other languages.


EDIT

From the 166 cultures that are reported in my system (CultureInfo.GetCultures(CultureTypes.SpecificCultures).Count()), it seems that only two separators are used: period and comma. You can try this in your system:

var seps = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
            .Select(ci => ci.NumberFormat.NumberDecimalSeparator)
            .Distinct()
            .ToList();

Assuming that this is true, this method may be helpful (note that the keyCode is OR'ed with the modifiers flag in order to eliminate invalid combinations):

 private bool IsDecimalSeparator(Keys keyCode, Keys modifiers)
 {
  Keys fullKeyCode = keyCode | modifiers;
  if (fullKeyCode.Equals(Keys.Decimal))          // value=110
   return true;

  string uiSep = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
  if (uiSep.Equals("."))
   return fullKeyCode.Equals(Keys.OemPeriod); // value=190
  else if (uiSep.Equals(","))
   return fullKeyCode.Equals(Keys.Oemcomma);  // value=188
  throw new ApplicationException(string.Format("Unknown separator found {0}", uiSep));
 }

A last note: According to Keys enumeration, the value 46 that you mention corresponds to the DEL (Delete) key (i.e. the point when Num Lock is OFF).

Panos
This doesn't answer the question when considering:"The NumberdecimalSeparator returns as 46, or '.' on my US system. Many other countries use ',' as the separator. The KeyDown event sets the KeyValue to 190 when I press the period."...as written in the question.
Jeff Yates
A: 

The problem here is that the values in the KeyEventArgs are key codes, not characters. If you handle KeyPress instead, you will get a char in the KeyPressEventArgs which you can use for the comparison.

Note: You should really compare the NumberDecimalSeparator characters as it is a string, not a single character so you need to consider scenarios where there is more than one character in the string.

Jeff Yates