C# 只能输入字母或数字

c# 只能输入字母或者数字 或者退格符 

方法一:KeyPress

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar != '\b') &&(!Char.IsLetter(e.KeyChar)) && (!char.IsDigit(e.KeyChar)))
            {
                e.Handled = true;
            }

        }

 

 

 

 

方法二: 正则表达式

添加引用

using System;
using System.Text.RegularExpressions;

 

posted @ 2018-03-05 16:55  笑笑小白  阅读(5102)  评论(0编辑  收藏  举报