Newer
Older
laserPTZ_CS / PlaceHolderTextBox.cs
wangxitong on 11 Sep 3 KB first commit
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace laserPTZ
{

    /// <summary>
    /// 带有PlaceHolder的Textbox
    /// </summary>
    /// <creator>marc</creator>
    public partial class PlaceHolderTextBox : TextBox
    {

        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }

        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;
            InitializeComponent();
        }

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

        /// <summary>
        /// 当焦点获得的时候,将显示提示文本
        /// </summary>
        private void RemovePlaceHolder()
        {
            if (_isPlaceHolder)
            {
                base.Text = "";
                this.ForeColor = Color.White;
                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();
        }
    }
}