Winform自定义控件 —— 水印文本框

        在开始阅读本文之前,如果您有学习创建自定义控件库并在其他项目中引用的需求,请参考:

在Visual Studio中创建自定义Winform控件库并在其他解决方案中引用https://blog.csdn.net/YMGogre/article/details/126508042


目录

1、应用场景: 

2、所需资源: 

3、源代码: 

4、使用方法: 

5、效果演示:


1、应用场景: 

  • 当我们需要文本框中有提示性文字告诉用户应当在当前文本框内输入何种内容时;比方说常见的密码栏会有诸如“请输入密码”这类提示性的文字: 

2、所需资源: 

(无,本质上就只是个 Label + TextBox 的组合控件,继承自 TextBox 类)

3、源代码: 

(有一些方法注释掉了,小伙伴们有额外功能需求的话可以按需取消一些代码注释) 

/* WatermarkTextBox.cs */

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

//注意命名空间修改为自己项目的命名空间
namespace WindowsFormsControlLibraryMadeByXJY
{
    public partial class WatermarkTextBox : TextBox
    {
        private readonly Label lblwaterText = new Label();

        public WatermarkTextBox()
        {
            InitializeComponent();
            lblwaterText.BorderStyle = BorderStyle.None;
            lblwaterText.Enabled = false;
            lblwaterText.BackColor = Color.White;
            lblwaterText.AutoSize = true;
            lblwaterText.Left = 2;
            lblwaterText.FlatStyle = FlatStyle.System;
            lblwaterText.Font = this.Font;
            lblwaterText.TextAlign = ContentAlignment.BottomLeft;
            Controls.Add(lblwaterText);
        }

        public override string Text
        {
            set
            {
                lblwaterText.Visible = value == string.Empty;
                base.Text = value;
            }
            get
            {
                return base.Text;
            }
        }

        /// <summary>
        /// 重写"控件上的 Size 属性值更改"事件处理方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSizeChanged(EventArgs e)
        {
            if (Multiline && (ScrollBars == ScrollBars.Vertical || ScrollBars == ScrollBars.Both))
                lblwaterText.Width = Width - 20;
            else
                lblwaterText.Width = Width;
            //lblwaterText.Height = Height - 2;
            lblwaterText.Top = (Height - lblwaterText.Height) / 2;
            base.OnSizeChanged(e);
        }

        /// <summary>
        /// 重写"文本改变"事件处理方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnTextChanged(EventArgs e)
        {
            //当base控件(TextBox)文本为空时,显示水印文字文本;不为空则不显示
            lblwaterText.Visible = base.Text == string.Empty;    
            base.OnTextChanged(e);
        }

        /// <summary>
        /// 重写"鼠标指针在控件上方并按下鼠标按钮"事件处理方法
        /// </summary>
        /// <param name="e"></param>
        //protected override void OnMouseDown(MouseEventArgs e)
        //{
        //    if(e.Button == MouseButtons.Left)
        //    {
        //        lblwaterText.Visible = false;
        //        base.OnMouseDown(e);
        //    }
        //}

        /// <summary>
        /// 重写"鼠标离开控件的可见部分"事件处理方法
        /// </summary>
        /// <param name="e"></param>
        //protected override void OnMouseLeave(EventArgs e)
        //{
        //    lblwaterText.Visible = base.Text == string.Empty;
        //    base.OnMouseLeave(e);
        //}

        /// <summary>
        /// 重写"控件成为该窗体的活动控件"事件处理方法
        /// </summary>
        /// <param name="e"></param>
        //protected override void OnEnter(EventArgs e)
        //{
        //    lblwaterText.Visible = false;
        //    base.OnEnter(e);
        //}

        /// <summary>
        /// 重写"控件不再是窗体的活动控件"事件处理方法
        /// </summary>
        /// <param name="e"></param>
        //protected override void OnLeave(EventArgs e)
        //{
        //    if (string.IsNullOrEmpty(base.Text))
        //        lblwaterText.Visible = true;
        //    base.OnLeave(e);
        //}

        [Category("扩展属性"), Description("显示的水印文字提示信息")]
        public string WaterText
        {
            get { return lblwaterText.Text; }
            set { lblwaterText.Text = value; }
        }

        [Category("扩展属性"), Description("水印文字的左上角相对于文本框左上角的坐标")]
        public Point WaterMarkLocation
        {
            get { return lblwaterText.Location; }
            set { lblwaterText.Location = value; }
        }

        [Category("扩展属性"), Description("水印文字字体")]
        public Font WaterMarkFont
        {
            get { return lblwaterText.Font; }
            set { lblwaterText.Font = value; }
        }
    }
}
/* WatermarkTextBox.Designer.cs */

using System.Runtime.CompilerServices;

//注意命名空间修改为自己项目的命名空间
namespace WindowsFormsControlLibraryMadeByXJY
{
    partial class WatermarkTextBox
    {
        /// <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);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);
        }

        #endregion

    }
}

4、使用方法: 

  • 水印文本框控件提供了三项可修改的扩展属性: 

  1. WaterMarkFont:设置水印文字字体;
  2. WaterMarkLocation:设置水印Label左上角相对于文本框左上角的坐标;
  3. WaterText:设置水印文字的文本;  

5、效果演示:

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值