文本框如何实现类似html的输入提示信息(html文本输入框提示文字)

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

文本框如何实现类似html的输入提示信息(html文本输入框提示文字)

那么这个是如何实现的,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();
        }
    }
}
文本框如何实现类似html的输入提示信息(html文本输入框提示文字)

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

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

文本框如何实现类似html的输入提示信息(html文本输入框提示文字)

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

文本框如何实现类似html的输入提示信息(html文本输入框提示文字)

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

祝您用餐愉快。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论