• winform textbox加水印效果


    首先创建一个用户控件名称为WatermakTextBox,让其继承textbox,代码如下:

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

    namespace UpdateSetting
    {
        public partial class WatermakTextBox : TextBox
        {
            private readonly Label lblwaterText = new Label();

            public WatermakTextBox()
            {
                InitializeComponent();
                lblwaterText.BorderStyle = BorderStyle.None;
                lblwaterText.Enabled = false;
                lblwaterText.BackColor =Color.White;
                lblwaterText.AutoSize = false;
                lblwaterText.Top = 1;
                lblwaterText.Left = 0;
                Controls.Add(lblwaterText);
            }

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

            public override string Text
            {
                set
                {
                    if (value != string.Empty)
                        lblwaterText.Visible = false;
                    else
                        lblwaterText.Visible = true;
                    base.Text = value;
                }
                get { return base.Text; }
            }

            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;
                base.OnSizeChanged(e);
            }

            protected override void OnEnter(EventArgs e)
            {
                lblwaterText.Visible = false;
                base.OnEnter(e);
            }

            protected override void OnLeave(EventArgs e)
            {
                if (base.Text == string.Empty)
                    lblwaterText.Visible = true;
                base.OnLeave(e);
            }

        }
    }

    然后将生成后的用户控件放到自己的项目中使用即可

  • 相关阅读:
    struts2(五) s标签和国际化
    struts2(四) ognl表达式、值栈、actionContext之间的关系
    数据结构(一) 单链表的实现-JAVA
    struts2(三) 输入校验和拦截器
    struts2(二) 表单参数自动封装和参数类型自动转换
    struts2(一) struts2入门
    Java Web(十四) 编写MyBookStore项目的总结
    Java Web(十三) 使用javamail进行发送邮件,(使用QQ,163,新浪邮箱服务器)
    Java中反射机制详解
    Java Web(十二) commons-fileupload上传下载
  • 原文地址:https://www.cnblogs.com/songling/p/2126583.html
Copyright © 2020-2023  润新知