WinForm控件之【TextBox】
基本介绍
文本控件,提供多行文本编辑和密码字符掩码功能。
常设置属性
ForeColor:此组件的前景色,用于显示文本;
BorderStyle:指示编辑控件是否应带有边框或边框类型;
Lines:多行编辑中的文本行,作为字符串值的数组;
MaxLength:指定可以在编辑控件中输入的最大字符数;
PasswordChar:指示将为单行编辑控件的密码输入显示的字符;
Multiline:控制编辑控件的文本是否能够跨越多行;
ScrollBars:定义控件滚动条的行为;
WordWrap:指示多行编辑控件是否自动换行;
Enabled:指示是否启用该控件,true为启用状态用户可编辑,false为禁用状态用户不可编辑;
Name:指示代码中用来标识该对象的名称;
Text:获取或设置多格式文本框中的文本;
事例举例
相关代码
//控件提示信息变量
string strUser = "长度不低于四个字符", strPwd = "长度不低于六个字符,由字母和数字组成";
#region 用户密码控件设置提示信息相关事件
private void txt_user_Enter(object sender, 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(object sender, 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(object sender, 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(object sender, 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(object sender, 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(object sender, EventArgs e)
{
this.Close();
}
转载于//www.cnblogs.com/ljhandsomeblog/p/11215249.html
还没有评论,来说两句吧...