Logo

C#窗体Winform,文本框TextBox如何实现类似html的PlaceHolder属性

photo

2022年07月06日

文章开始之前,先看下效果图:

那么这个是如何实现的,Winform自带的TextBox是不具备这种PlaceHolder提示功能的,要实现它,我们就需要重写TextBox控件。

具体代码如下:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RevitDevelopment.CustomControls
{
    /// <summary>
    /// 带有PlaceHolder的Textbox
    /// </summary>
    /// <creator>marc</creator>
    public class PlaceHolderTextBox : TextBox
    {
        private bool _isPlaceHolder = true;
        private string _placeHolderText;
        /// <summary>
        /// 提示文本
        /// </summary>
        public string PlaceHolderText
        {
            get { return _placeHolderText; }
            set
            {
                _placeHolderText = value;
                SetPlaceholder();
            }
        }

        /// <summary>
        /// 文本
        /// </summary>
        public new string Text
        {
            get
            {
                return _isPlaceHolder ? string.Empty : base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public PlaceHolderTextBox()
        {
            GotFocus += RemovePlaceHolder;
            LostFocus += SetPlaceholder;
        }

        /// <summary>
        /// 当焦点失去的时候,将清空提示文本
        /// </summary>
        private void SetPlaceholder()
        {
            if (string.IsNullOrEmpty(base.Text))
            {
                base.Text = PlaceHolderText;
                this.ForeColor = Color.Gray;
                this.Font = new Font(this.Font, FontStyle.Italic);
                _isPlaceHolder = true;
            }
        }

        /// <summary>
        /// 当焦点获得的时候,将显示提示文本
        /// </summary>
        private void RemovePlaceHolder()
        {
            if (_isPlaceHolder)
            {
                base.Text = "";
                this.ForeColor = SystemColors.WindowText;
                this.Font = new Font(this.Font, FontStyle.Regular);
                _isPlaceHolder = false;
            }
        }

        /// <summary>
        /// 失去焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetPlaceholder(object sender, EventArgs e)
        {
            SetPlaceholder();
        }

        /// <summary>
        /// 获得焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RemovePlaceHolder(object sender, EventArgs e)
        {
            RemovePlaceHolder();
        }
    }
}

代码是简单明了的,它提供了属性PlaceHolderText,用于填写提示信息,当焦点获得或者失去时,将触发事件。

将该代码,写好后编译,将会在“工具箱”中出现这个组件:

将该组件拖入您想要使用的地方,然后设置如下属性:

如此,便可以实现开篇的效果图。

 

祝您用餐愉快。

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
本文为原创文章,请注意保留出处!

做完测试后,都会编写一份测试报告,测试报告中最主要的就是呈现出测试结果,哪些用例通过了,哪些用例...postman—postman生成测试报告

热门文章

Windows Server IIS+ARR反向代理(配置反向代理服务器) 1.概念说明:反向代理反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相...WindowsServerIIS+ARR反向代理(配置反向代理服务器) 作者:Pastore Antonio
1581 浏览量
ffmpeg 生成水印 1:先要配置ffmpeg的滤镜:参考:https://www.jianshu.com/p/9d24...ffmpeg生成水印 作者:Pastore Antonio
1508 浏览量
C#中List的FindAll方法的正确打开方式 初略的介绍一种常见的List写法,这种写法在3.0以后其实是很简单的,但是在2.0左右的系统运用中还...C#中List的FindAll方法的正确打开方式 作者:Pastore Antonio
1467 浏览量
IntelliJ IDEA 代码字体大小的快捷键设置放大缩小(很实用)(图文详解) 这是在设置IntelliJIDEA...IntelliJIDEA代码字体大小的快捷键设置放大缩小(很实用)(图文详解) 作者:Pastore Antonio
1461 浏览量
Navicat Premium 12.0.22 安装与破解 一、安装  NavicatPremium12.0.22的下载链接:https://pan.ba...NavicatPremium12.0.22安装与破解 作者:Pastore Antonio
1447 浏览量