TextBox1_KeyPress 将不再工作,即使我没有更改任何代码

TextBox1_KeyPress will not work anymore even though I haven't changed any code

最近我从 Visual Studio express 2013 切换到 Visual Studio Community 15。 当我用这段代码 运行 我的表单应该阻止字符输入时,文本框只允许数字和小数 (.) 并且它 运行 完美,但是一旦我切换到社区 15它不再阻止字符输入。为什么?只要它是数字或小数,handled 就设置为 false?

private void WeeklyCheckTxtBox_KeyPress(object sender, KeyPressEventArgs e)
    {
       if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsPunctuation('.'))
        {
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
        }
    }

问题是 char.IsPunctuation('.') 将始终 return 为真,因为 .毫无疑问是标点符号,因此事件将始终未处理 - 我认为您可能打算写 e.KeyChar == '.'char.IsPunctuation(e.KeyChar).