//控件提示信息变量
string strUser = "长度不低于四个字符", strPwd = "长度不低于六个字符,由字母和数字组成";#region 用户密码控件设置提示信息相关事件
private void txt_user_Enter(objectsender, EventArgs e)
{
TextBox tb=(TextBox)sender;if(tb.Text.Equals(strUser))
{
tb.Text= string.Empty;
tb.ForeColor=System.Drawing.SystemColors.WindowText;
}
}private void txt_user_Leave(objectsender, EventArgs e)
{
TextBox tb=(TextBox)sender;if (string.IsNullOrWhiteSpace(tb.Text))
{
tb.Text=strUser;
tb.ForeColor=System.Drawing.SystemColors.ScrollBar;
}
}private void txt_pwd_Enter(objectsender, EventArgs e)
{
TextBox tb=(TextBox)sender;if(tb.Text.Equals(strPwd))
{
tb.Text= string.Empty;
tb.ForeColor=System.Drawing.SystemColors.WindowText;
tb.PasswordChar= '*';
}
}private void txt_pwd_Leave(objectsender, EventArgs e)
{
TextBox tb=(TextBox)sender;if (string.IsNullOrWhiteSpace(tb.Text))
{
tb.Text=strPwd;
tb.ForeColor=System.Drawing.SystemColors.ScrollBar;
tb.PasswordChar= '\0';
}
}#endregion
//登录
private void btn_login_Click(objectsender, EventArgs e)
{if (string.IsNullOrWhiteSpace(txt_user.Text) ||txt_user.Text.Equals(strUser))
{
MessageBox.Show("用户名不能为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;
}else{if (txt_user.Text.Length < 4)
{
MessageBox.Show("用户名长度不能低于四个字符!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;
}
}if (string.IsNullOrWhiteSpace(txt_pwd.Text) ||txt_pwd.Text.Equals(strPwd))
{
MessageBox.Show("密码不能为空!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;
}else{if (txt_pwd.Text.Length < 6)
{
MessageBox.Show("用户名长度不能低于六个字符!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;
}string strRegex = "[0-9]";if (!System.Text.RegularExpressions.Regex.IsMatch(txt_pwd.Text, strRegex))
{
MessageBox.Show("密码必须存在数字,请确认!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;
}
strRegex= "[a-zA-Z]";if (!System.Text.RegularExpressions.Regex.IsMatch(txt_pwd.Text, strRegex))
{
MessageBox.Show("密码必须存在字母,请确认!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;
}
}
MessageBox.Show("登录成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}//退出
private void btn_exit_Click(objectsender, EventArgs e)
{this.Close();
}