VB.NET
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = Not "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".Contains(e.KeyChar)
'如果要只允许输入数字,就把上一行字符串中的字母删除
End Sub
C#
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !"01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".Contains(e.KeyChar);
//如果要只允许输入数字,就把上一行字符串中的字母删除
}