使用keyPress事件效果比keydown效果要好
private void txtDBThempLibName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Back) {
e.Handled = false;
return;
}
string pattern = @"^[a-zA-Z0-9]{0,6}$";
if (!Regex.IsMatch(txtDBThempLibName.Text.Trim(), pattern))
{
MessageBox.Show("輸入格式不正確,請檢查:\r\n請檢查輸入是否爲數字或字母以外的符號");
return;
}
if (txtDBThempLibName.Text.Trim().Length>0)
btnImport.Enabled = true;
else
btnImport.Enabled = false;
if (txtDBThempLibName.Text.Length >= 6) e.Handled = true;
}