How to define NumberDecimalSeparator in KeyDown event (C #)

I am trying to find out if the user clicked the decimal separator in the text box and enable or disable it depending on other parameters.

NumberdecimalSeparator is returned as 46, or '.' in my american system. Many other countries use "," as a delimiter. The KeyDown event sets KeyValue to 190 when I press the period.

Am I just continuing to search for commas / periods, or is there a better way?

+5
source share
2 answers

Call

CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator

. , .


166 , (CultureInfo.GetCultures(CultureTypes.SpecificCultures).Count()), , : . :

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

, , ( , keyCode OR'd modifiers ):

    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));
    }

: , 46, , DEL () ( , Num Lock ).

+9

, KeyEventArgs , . KeyPress , char KeyPressEventArgs, .

. NumberDecimalSeparator, , , , .

0

All Articles