heisetoufa 发表于 2013-1-27 05:13:05

c# winform 用正则,实现只能输入英文和数字

string pattern = @"^+$";//正则式子                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;
 
黑色头发  http://heisetoufa.iteye.com
页: [1]
查看完整版本: c# winform 用正则,实现只能输入英文和数字