Detect decimal separator

前端 未结 3 1841
太阳男子
太阳男子 2020-12-08 18:31

I have to detect decimal separator in current windows setting. Im using visual studio 2010, windows form. In particular, if DecimalSeparator is comma, if user input dot in t

相关标签:
3条回答
  • 2020-12-08 19:04

    Actually you should be using

    Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator
    

    instead of

    CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator
    

    Using the second one gives you the OS default settings, which might be different then user Regional Locales for particular user account logged to this PC

    0 讨论(0)
  • 2020-12-08 19:08

    Solution:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        char a = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
        if (e.KeyChar == a)
        {
            e.Handled = true;
            textBox1.Text = "0";
        }
    }
    

    That way, when you hit . or , you will have a 0 in your TextBox.

    EDIT:

    If you want to insert a 0 everytime you hit the decimal separator, this is the code:

    char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
    if (e.KeyChar == a)
    {
        e.KeyChar = '0';
    }
    
    0 讨论(0)
  • 2020-12-08 19:13

    You shouldn't use a while loop, I think it will freeze the application, use if instead, the problem might be here

    0 讨论(0)
提交回复
热议问题