【问题标题】:Detect decimal separator检测小数分隔符
【发布时间】:2013-01-25 00:38:16
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • “但不起作用”并没有告诉我们出了什么问题。
  • 由于我们无法从这里读懂您的想法或看到您的屏幕,如果您能解释一下“不工作”的含义,那将非常有用。
  • Vincenzolopalo,对于初学者来说,你不需要一个 while 循环,你需要更好地解释你想要什么......你是说你不想允许“,”也在检查== vs .Equals 你应该知道什么时候在比较对象时通常使用一个,.Equals 会起作用.. 无论哪种方式.. 你当前的逻辑有缺陷,为什么不使用蒙面编辑..?
  • 要解决您的问题,只需使用MaskedTextBox <==click here 了解更多信息
  • 这个数字是有意义的,它告诉我 1500.00 你想代表货币..?你也可以解决这个问题

标签: c# globalization


【解决方案1】:

解决方案:

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

这样,当您点击 ., 时,您的 TextBox 中将有一个 0

编辑:

如果您想在每次点击小数点分隔符时插入0,代码如下:

char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (e.KeyChar == a)
{
    e.KeyChar = '0';
}

【讨论】:

  • 如果您需要一个用户只能输入数字的文本框,我有代码.. MaskedTextboxes 也是一种解决方案,但我不喜欢它们。
  • 感谢安德烈斯,这确实是我需要的解决方案!! :) 但可以完美地使用这个: CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator
  • 当然!很高兴帮助你:)
【解决方案2】:

其实你应该使用

CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator

而不是

CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator

使用第二个为您提供操作系统默认设置,这可能与登录到此 PC 的特定用户帐户的用户区域区域设置不同。

感谢berhirGrimm 指出[docs]

【讨论】:

  • 我不认为这是正确的。这就是docs 所说的:The current culture is a property of the executing thread. Retrieving the value of the CultureInfo.CurrentCulture property is a more performant equivalent of retrieving the CultureInfo object returned by the Thread.CurrentThread.CurrentCulture property.
  • berhir 是对的。文档明确建议使用 CultureInfo:The current culture is a property of the executing thread. When you set this property to a CultureInfo object that represents a new culture, the value of the Thread.CurrentThread.CurrentCulture property also changes. However, we recommend that you always use the CultureInfo.CurrentCulture property to retrieve and set the current culture.
  • 那是前段时间,但我想我要不要使用 CurrentUICulture(里面有 UI)。但我同意你已经找到了更高效的方法。感谢您发现这一点。
【解决方案3】:

您不应该使用while 循环,我认为它会冻结应用程序,请改用if,问题可能就在这里

【讨论】:

  • 更改的代码在哪里..?在您的原始问题中看起来相同
  • 你确定46是点码吗?你也可以试试这个测试:e.KeyChar == '.'
  • ppetrov that do = "." 这样测试你会看到var somChar = (char)46
  • 我相信你@DJ KRAZE,只是没有测试它,并认为这样字符代码不会有任何错误。但正如您在之前的评论中所说,代码的主要问题是.Equals
  • 不是问题 pptrov .. 但他最初的问题在于一些事情 ..1 使用他不需要的 While 循环,检查他永远找不到的“”,因为他需要检查 Key , 3rd 使用不正确的 Control,他应该使用 MaskedEdit 代替
猜你喜欢
  • 1970-01-01
  • 2021-12-17
  • 2016-03-22
  • 2016-12-08
  • 2011-04-21
  • 1970-01-01
  • 2014-07-08
  • 2013-11-07
  • 1970-01-01
相关资源
最近更新 更多