Winform的TextBox控件显示默认文本点击消失离开显示

本文介绍如何在Winform应用中设置TextBox控件的默认文本,并使其在点击时消失,失去焦点或鼠标离开时恢复显示。通过Enter和Leave事件或Mouse事件可以实现这一功能。
摘要由CSDN通过智能技术生成

设置TextBox控件的默认文本即在加载TextBox控件时为其Text文本赋值。点击文本消失,离开或者失去焦点显示默认文本可以通过Enter和Leave事件或Mouse的事件来实现。具体代码如下。

private void txtPassword_Enter(object sender, EventArgs e)
        {
            pnlPassword.BackgroundImage = ImageHelper.GetImage("Login\\login_account_focus.png");
            pnlAccount.BackgroundImage = ImageHelper.GetImage("Login\\login_account_default.png");

            if (txtPassword.Text.Trim() == "请输入密码")
            {
                txtPassword.ForeColor = ColorTranslator.FromHtml("#333333");
                txtPassword.Text = "";
                txtPassword.PasswordChar = '*';
            }
        }

        private void txtPassword_Leave(object sender, EventArgs e)
        {
            pnlPassword.BackgroundImage = ImageHelper.GetImage("Login\\login_account_default.png");

            if (txtPassword.Text.Trim().Length == 0)
            {
                txtPassword.ForeColor = ColorTranslator.FromHtml("#999999");
                txtPassword.PasswordChar = '\0';
                txtPassword.Text = "  请输入密码";
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仰望星空的代码

创作不易,您的支持是我的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值