如何检测 KeyDown 事件中的 NumberDecimalSeparator (C#)

发布于 2024-07-07 12:56:07 字数 198 浏览 8 评论 0原文

我试图查看用户是否在文本框中按下了小数分隔符,并根据其他参数允许或禁止它。

NumberdecimalSeparator 返回 46 或“.” 在我的美国系统上。 许多其他国家/地区使用“,”作为分隔符。 当我按下句号时,KeyDown 事件将 KeyValue 设置为 190。

我是否只是继续寻找逗号/句号,还是有更好的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彩虹直至黑白 2024-07-14 12:56:07

该调用

CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator

获取当前用户界面区域性的小数点分隔符。 您可以使用其他文化来获取其他语言的分隔符。


编辑

从我的系统中报告的 166 种文化 (CultureInfo.GetCultures(CultureTypes.SpecificCultures).Count()) 来看,似乎只使用了两个分隔符:句点和逗号。 您可以在您的系统中尝试此操作:

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

假设这是真的,此方法可能会有所帮助(请注意,keyCodemodifiers 标志进行 OR 运算,以便消除无效组合):

    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关闭时的点)。

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).

枕头说它不想醒 2024-07-14 12:56:07

这里的问题是 KeyEventArgs 中的值是按键代码,而不是字符。 如果您改为处理 KeyPress,您将在 KeyPressEventArgs 中获得一个可用于比较的字符。

注意:您确实应该比较 NumberDecimalSeparator 字符,因为它是一个字符串,而不是单个字符,因此您需要考虑字符串中存在多个字符的情况。

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.

夜巴黎 2024-07-14 12:56:07

如果您需要知道按下的字符是否是小数分隔符:

private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
    char separator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
    if (e.KeyCahr == separador)
    {
        // true
    }
    else
    {
        // false
    }
}

但是,如果您需要接受小数数字键盘键作为任何区域性的小数分隔符:

    private bool decimalSeparator = false;
    private void Control_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Decimal)
            decimalSeparator = true;
    }

    private void Control_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (decimalSeparator)
        {
            e.KeyChar = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
            decimalSeparator = false;
        }
    }

If you need know if the char pressed is decimal separator:

private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
    char separator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
    if (e.KeyCahr == separador)
    {
        // true
    }
    else
    {
        // false
    }
}

But, if you need acept decimal numpad key as decimal separator of any culture:

    private bool decimalSeparator = false;
    private void Control_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Decimal)
            decimalSeparator = true;
    }

    private void Control_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (decimalSeparator)
        {
            e.KeyChar = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
            decimalSeparator = false;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文