博客
关于我
(三十一)c#Winform自定义控件-文本框(四)-HZHControls
阅读量:417 次
发布时间:2019-03-06

本文共 2920 字,大约阅读时间需要 9 分钟。

自定义美观文本框控件

随着项目的深入,我一直想为项目开发一套高质量的自定义控件,最终有了这篇关于自定义文本框控件的实现方案。

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。


GitHub

如果觉得写的还行,请点个 star 支持一下吧


目录

  • 准备工作
  • 开始
  • 属性
  • 事件
  • 用处及效果
  • 最后的话

  • 准备工作

    终于到文本框了,文本框将包含原文本框扩展,透明文本框,数字输入文本框,带边框文本框。本文将讲解带边框文本框,可选弹出键盘样式,继承自控件基类UCControlBase。同时用到了无焦点窗体和键盘,如果你还没有了解,请前往查看。


    开始

    添加用户控件,命名UCTextBoxEx,继承自UCControlBase。

    下面是该控件的主要属性:

    属性

    • 是否显示清理按钮

      public bool IsShowClearBtn  {      get { return m_isShowClearBtn; }      set      {          m_isShowClearBtn = value;          if (value)          {              btnClear.Visible = !(txtInput.Text == "\r\n") && !string.IsNullOrEmpty(txtInput.Text);          }          else          {              btnClear.Visible = false;          }      }  }
    • 是否显示查询按钮

      public bool IsShowSearchBtn  {      get { return m_isShowSearchBtn; }      set      {          m_isShowSearchBtn = value;          btnSearch.Visible = value;      }  }
    • 键盘打开样式

      public KeyBoardType KeyBoardType  {      get { return keyBoardType; }      set { keyBoardType = value; }  }
    • 字体

      public new Font Font  {      get { return this.txtInput.Font; }      set { this.txtInput.Font = value; }  }
    • 输入类型

      public TextInputType InputType  {      get { return txtInput.InputType; }      set { txtInput.InputType = value; }  }
    • 水印文字

      public string PromptText  {      get { return this.txtInput.PromptText; }      set { this.txtInput.PromptText = value; }  }
    • 水印字体

      public Font PromptFont  {      get { return this.txtInput.PromptFont; }      set { this.txtInput.PromptFont = value; }  }
    • 水印颜色

      public Color PromptColor  {      get { return this.txtInput.PromptColor; }      set { this.txtInput.PromptColor = value; }  }
    • 正则表达式

      public string RegexPattern  {      get { return this.txtInput.RegexPattern; }      set { this.txtInput.RegexPattern = value; }  }
    • 最大值

      public decimal MaxValue  {      get { return this.txtInput.MaxValue; }      set { this.txtInput.MaxValue = value; }  }
    • 最小值

      public decimal MinValue  {      get { return this.txtInput.MinValue; }      set { this.txtInput.MinValue = value; }  }
    • 小数位数

      public int DecLength  {      get { return this.txtInput.DecLength; }      set { this.txtInput.DecLength = value; }  }

    事件

    • 文本改变事件

      public new event EventHandler TextChanged  {      add: TextChanged += new EventHandler(TextChangedHandler);      remove: TextChanged -= new EventHandler(TextChangedHandler);  }
    • 键盘按钮点击事件

      public event EventHandler KeyboardClick  {      add: KeyboardClick += new EventHandler(KeyboardClickHandler);      remove: KeyboardClick -= new EventHandler(KeyboardClickHandler);  }
    • 查询按钮点击事件

      public event EventHandler SearchClick  {      add: SearchClick += new EventHandler(SearchClickHandler);      remove: SearchClick -= new EventHandler(SearchClickHandler);  }

    用处及效果

    这套文本框控件具有以下功能:

  • 清理按钮:支持清除文本框内容。
  • 查询按钮:触发搜索点击事件。
  • 弹出键盘:支持多种键盘样式,包括标准键盘、数字键盘和手写键盘。
  • 水印效果:支持自定义水印文字、字体和颜色。
  • 正则表达式验证:支持通过正则表达式限制输入格式。
  • 最大值和最小值限制:适用于数字输入场景。
  • 焦点变色:支持根据设置改变焦点时的背景颜色。

  • 最后的话

    如果你觉得这篇文章写得还行,请点个 star 支持一下吧。

    欢迎前来交流探讨:

    • GitHub:
    • 代码云:
    • 企鹅群:568015492

    转载地址:http://hfvkz.baihongyu.com/

    你可能感兴趣的文章
    PanTools多网盘登录神器
    查看>>
    Papyrus项目常见问题解决方案
    查看>>
    Parallel.ForEach使用示例
    查看>>
    Parallel.ForEach的基础使用
    查看>>
    parallels desktop for mac安装虚拟机 之parallelsdesktop密钥 以及 parallels desktop安装win10的办公推荐可以提高办公效率...
    查看>>
    parallelStream导致LinkedList遍历时空指针的问题
    查看>>
    Parameter ‘password‘ not found. Available parameters are [md5String, param1, username, param2]
    查看>>
    ParameterizedThreadStart task
    查看>>
    Paramiko exec_命令的实时输出
    查看>>
    Spring security之管理session
    查看>>
    paramiko模块
    查看>>
    param[:]=param-lr*param.grad/batch_size的理解
    查看>>
    spring mvc excludePathPatterns失效 如何解决spring拦截器失效 excludePathPatterns忽略失效 拦截器失效 spring免验证拦截器不起作用
    查看>>
    Spring Cloud 之注册中心 EurekaServerAutoConfiguration源码分析
    查看>>
    Parrot OS 6.2 重磅发布!推出全新 Docker 容器启动器
    查看>>
    Parrot OS 6.3 发布!全面提升安全性,新增先进工具,带来更高性能
    查看>>
    ParseChat应用源码ios版
    查看>>
    Part 2异常和错误
    查看>>
    Pascal Script
    查看>>
    Spring Boot集成Redis实现keyspace监听 | Spring Cloud 34
    查看>>