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

本文关键字:代码 任何 KeyPress 不再 工作 TextBox1 | 更新日期: 2023-09-27 18:21:30

最近我从Visual Studio express 2013转到了Visual Studio Community 15。当我用这段应该防止字符输入的代码运行我的表单时,文本框只允许数字和小数(.),它运行得很好,但一旦我切换到Community 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;
        }
    }

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

问题是这个字符。Is标点符号('.')将始终返回true作为。毫无疑问是一个标点符号,因此事件总是无法处理的——我想你可能想写e.KeyChar == '.'char.IsPunctuation(e.KeyChar)

相关文章: