C# 文本框输入提示

在我们窗体编程时,有时需要实现输入文本框的提示效果,实现方法如下:
1、窗体中的文本框名称分别为txtName和txtPwd

2、控制其事件:
#region 文本框输入提示
Boolean textboxHasText = false;//判断输入框是否有文本
private void txtName_Enter(object sender, EventArgs e)
{
if (textboxHasText == false)
{
txtName.Text = “”;
txtName.ForeColor = Color.Black;
}
}
private void txtName_Leave(object sender, EventArgs e)
{
if (txtName.Text == “”)
{
txtName.Text = “请输入用户名”;
txtName.ForeColor = Color.LightGray;
textboxHasText = false;
}
else
{
textboxHasText = true;
}
}
Boolean textboxHasText2 = false;//判断输入框是否有文本
private void txtPwd_Enter(object sender, EventArgs e)
{
if (textboxHasText2 == false)
{
txtPwd.Text = “”;
txtPwd.PasswordChar = (char)42;
txtPwd.ForeColor = Color.Black;
}
}
private void txtPwd_Leave(object sender, EventArgs e)
{
if (txtPwd.Text == “”)
{
txtPwd.Text = “请输入密码”;
txtPwd.ForeColor = Color.LightGray;
textboxHasText2 = false;
txtPwd.PasswordChar = ‘\0’;
}
else
{
textboxHasText2 = true;
}
}
#endregion
3、实现效果:
实现效果

猜你喜欢

转载自blog.csdn.net/qq_30725967/article/details/85257261
今日推荐