using System.Text .RegularExpressions;
private void txtChequeCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter )
{
if(Regex.Match (this.txtChequeCode .Text .Trim (),@"^/d+$").Success )
{
this.txtChequeSum .Focus ();
this.txtChequeSum .Select ();
}
else
{
MessageBox.Show ("输入无效!,只允许输入数字");
}
}
}
private void txtChequeSum_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter )
{
if(Regex.Match (this.txtChequeSum .Text .Trim (),@"^/d+$").Success )
{
this.ButtonSave .Focus ();
this.ButtonSave .Select ();
}
else
{
MessageBox.Show ("输入无效!,只允许输入数字");
}
}
}
博客给出两段代码,分别是 txtChequeCode 和 txtChequeSum 文本框的 KeyDown 事件处理。当按下回车键时,使用正则表达式验证输入内容是否为数字,若为数字则聚焦到下一个控件,否则弹出提示框告知输入无效。
2009





