C# 只能输入字母或数字

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

方法一:KeyPress

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

        }

方法二: 正则表达式

添加引用

using System;
using System.Text.RegularExpressions;

原文地址:https://www.cnblogs.com/rosesmall/p/8509929.html