我想知道是否有让文本框只接受数字的正确方法。
例如,我希望它“阻止”用户填写“abcd1234”,只让他填写“1234”。
最佳答案
我尝试了以下代码并且对我来说效果很好。 文本框
将允许用户输入仅数字。
private void txtbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
你也可以试试这个
e.Handled = !(char.IsDigit(e.KeyChar));
关于c# - 如何让 TextBox 只接受数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37805179/