给上次问如何屏蔽数字键的朋友
在论坛上看到问如何让textBox不能输入数字。使用两个事件处理一下就可以了。代码如下:
程序代码:
public partial class Form1 : Form { private string initString = null; private bool hasEnterNumber = false; public Form1() { InitializeComponent(); this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); this.textBox1.TextChanged += new EventHandler(textBox1_TextChanged); } void textBox1_TextChanged(object sender, EventArgs e) { if (this.hasEnterNumber) { this.textBox1.Text = this.initString; this.hasEnterNumber = false; this.textBox1.SelectionStart = this.textBox1.Text.Length; } } void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if ( (int)e.KeyChar >= (int)Keys.D0 && (int)e.KeyChar <= (int)Keys.D9) { this.hasEnterNumber = true; initString = this.textBox1.Text; } } }