string pattern = @"^[a-zA-Z0-9]+$";//正则式子 string param1 = null; Match m = Regex.Match(this.textBox1.Text, pattern); // 匹配正则表达式,把this.textBox1.Text跟pattern正则对比 if (!m.Success) // 判断输入的是不是英文和数字,不是进入 { param1 = this.textBox1.Text;//将现在textBox的值保存下来 // 将光标定位到文本框的最后 this.textBox1.SelectionStart = this.textBox1.Text.Length; } else //输入的是英文和数字 { param1 = this.textBox1.Text; // 将现在textBox的值保存下来 }
要引入using System.Text.RegularExpressions;