本文介绍了检测小数点分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在当前Windows设置中检测小数点分隔符。我正在使用Visual Studio 2010,Windows窗体。特别是,如果DecimalSeparator是逗号,如果用户在textbox1中输入点,则需要在textbox2中显示零。

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 textbox1, I need show zero in textbox2.

我尝试使用此代码,但不起作用:

I tryed with this code, but not works:

private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)
    {
        string uiSep = CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
        if (uiSep.Equals(","))
        {
            while (e.KeyChar == (char)46)
            {
                tbxConvertito.Text = "0";
            }
        }
    }

我也尝试过此代码,但不起作用:

I have tryed also this code, but not work:

private void tbxDaConvertire_KeyPress(object sender, KeyPressEventArgs e)
    {
        string uiSep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
        if (uiSep.Equals(","))
        {
            if (e.KeyChar == (char)46)
            {
                tbxConvertito.Text = "0";
            }
        }
    }


推荐答案

解决方案:

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

这样,当您按下,您的文本框中将有 0

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

编辑:

如果您想每次插入 0 击中小数点分隔符,这是代码:

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

这篇关于检测小数点分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 16:05