我需要检测当前 Windows 系统设置中的小数分隔符。我正在使用 Visual Studio 2010 和 Windows Form。特别是,如果小数分隔符是逗号,并且用户在文本框1中输入点,则需要在文本框2中显示零。
我尝试了以下代码,但没有起作用:
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";
}
}
}
==
与.Equals
时不想要允许“,”吗?一般来说,在比较对象时使用.Equals
就可以了。总之,您当前的逻辑有缺陷,为什么不使用 Masked Edit 呢? - MethodMan<==点击此处
了解更多信息。 - MethodMan