官网

http://www.hzhcontrols.com

前提

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

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

欢迎前来交流探讨: 企鹅群568015492 

idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

终于到文本框了,文本框将包含原文本框扩展,透明文本框,数字输入文本框,带边框文本框

本文将讲解原文本框扩展,主要增加水印和输入控制

开始

添加一个组件,命名TextBoxEx,继承TextBox

属性

 private bool blnFocus = false;

         private string _promptText = string.Empty;

         private Font _promptFont = new Font("微软雅黑", 15f, FontStyle.Regular, GraphicsUnit.Pixel);

         private Color _promptColor = Color.Gray;

         private Rectangle _myRectangle = Rectangle.FromLTRB(, , , );

         private TextInputType _inputType = TextInputType.NotControl;

         private string _regexPattern = "";

         private string m_strOldValue = string.Empty;

         private decimal _maxValue = 1000000m;

         private decimal _minValue = -1000000m;

         private int _decLength = ;

         /// <summary>
/// 水印文字
/// </summary>
[Description("水印文字"), Category("自定义")]
public string PromptText
{
get
{
return this._promptText;
}
set
{
this._promptText = value;
this.OnPaint(null);
}
} [Description("水印字体"), Category("自定义")]
public Font PromptFont
{
get
{
return this._promptFont;
}
set
{
this._promptFont = value;
}
} [Description("水印颜色"), Category("自定义")]
public Color PromptColor
{
get
{
return this._promptColor;
}
set
{
this._promptColor = value;
}
} public Rectangle MyRectangle
{
get;
set;
} public string OldText
{
get;
set;
} [Description("获取或设置一个值,该值指示文本框中的文本输入类型。")]
public TextInputType InputType
{
get
{
return this._inputType;
}
set
{
this._inputType = value;
if (value != TextInputType.NotControl)
{
TextChanged -= new EventHandler(this.TextBoxEx_TextChanged);
TextChanged += new EventHandler(this.TextBoxEx_TextChanged);
}
else
{
TextChanged -= new EventHandler(this.TextBoxEx_TextChanged);
}
}
}
/// <summary>
/// 获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。
/// </summary>
[Description("获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。")]
public string RegexPattern
{
get
{
return this._regexPattern;
}
set
{
this._regexPattern = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最大值
/// </summary>
[Description("当InputType为数字类型时,能输入的最大值。")]
public decimal MaxValue
{
get
{
return this._maxValue;
}
set
{
this._maxValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,能输入的最小值。")]
public decimal MinValue
{
get
{
return this._minValue;
}
set
{
this._minValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,小数位数。")]
public int DecLength
{
get
{
return this._decLength;
}
set
{
this._decLength = value;
}
}

一些事件

 void TextBoxEx_KeyPress(object sender, KeyPressEventArgs e)
{
//以下代码 取消按下回车或esc的“叮”声
if (e.KeyChar == System.Convert.ToChar() || e.KeyChar == System.Convert.ToChar())
{
e.Handled = true;
}
} private void TextBoxEx_MouseUp(object sender, MouseEventArgs e)
{
if (this.blnFocus)
{
base.SelectAll();
this.blnFocus = false;
}
} private void TextBoxEx_GotFocus(object sender, EventArgs e)
{
this.blnFocus = true;
base.SelectAll();
} private void TextBoxEx_TextChanged(object sender, EventArgs e)
{
if (this.Text == "")
{
this.m_strOldValue = this.Text;
}
else if (this.m_strOldValue != this.Text)
{
if (!ControlHelper.CheckInputType(this.Text, this._inputType, this._maxValue, this._minValue, this._decLength, this._regexPattern))
{
int num = base.SelectionStart;
if (this.m_strOldValue.Length < this.Text.Length)
{
num--;
}
else
{
num++;
}
base.TextChanged -= new EventHandler(this.TextBoxEx_TextChanged);
this.Text = this.m_strOldValue;
base.TextChanged += new EventHandler(this.TextBoxEx_TextChanged);
if (num < )
{
num = ;
}
base.SelectionStart = num;
}
else
{
this.m_strOldValue = this.Text;
}
}
}

重绘

  protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (string.IsNullOrEmpty(this.Text) && !string.IsNullOrEmpty(this._promptText))
{
if (e == null)
{
using (Graphics graphics = Graphics.FromHwnd(base.Handle))
{
if (this.Text.Length == && !string.IsNullOrEmpty(this.PromptText))
{
TextFormatFlags textFormatFlags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
if (this.RightToLeft == RightToLeft.Yes)
{
textFormatFlags |= (TextFormatFlags.Right | TextFormatFlags.RightToLeft);
}
TextRenderer.DrawText(graphics, this.PromptText, this._promptFont, base.ClientRectangle, this._promptColor, textFormatFlags);
}
}
}
}
}

下面是完整代码

 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:TextBoxEx.cs
// 创建日期:2019-08-15 16:03:44
// 功能描述:TextBox
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace HZH_Controls.Controls
{
public partial class TextBoxEx : TextBox
{
private bool blnFocus = false; private string _promptText = string.Empty; private Font _promptFont = new Font("微软雅黑", 15f, FontStyle.Regular, GraphicsUnit.Pixel); private Color _promptColor = Color.Gray; private Rectangle _myRectangle = Rectangle.FromLTRB(, , , ); private TextInputType _inputType = TextInputType.NotControl; private string _regexPattern = ""; private string m_strOldValue = string.Empty; private decimal _maxValue = 1000000m; private decimal _minValue = -1000000m; private int _decLength = ; /// <summary>
/// 水印文字
/// </summary>
[Description("水印文字"), Category("自定义")]
public string PromptText
{
get
{
return this._promptText;
}
set
{
this._promptText = value;
this.OnPaint(null);
}
} [Description("水印字体"), Category("自定义")]
public Font PromptFont
{
get
{
return this._promptFont;
}
set
{
this._promptFont = value;
}
} [Description("水印颜色"), Category("自定义")]
public Color PromptColor
{
get
{
return this._promptColor;
}
set
{
this._promptColor = value;
}
} public Rectangle MyRectangle
{
get;
set;
} public string OldText
{
get;
set;
} [Description("获取或设置一个值,该值指示文本框中的文本输入类型。")]
public TextInputType InputType
{
get
{
return this._inputType;
}
set
{
this._inputType = value;
if (value != TextInputType.NotControl)
{
TextChanged -= new EventHandler(this.TextBoxEx_TextChanged);
TextChanged += new EventHandler(this.TextBoxEx_TextChanged);
}
else
{
TextChanged -= new EventHandler(this.TextBoxEx_TextChanged);
}
}
}
/// <summary>
/// 获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。
/// </summary>
[Description("获取或设置一个值,该值指示当输入类型InputType=Regex时,使用的正则表达式。")]
public string RegexPattern
{
get
{
return this._regexPattern;
}
set
{
this._regexPattern = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最大值
/// </summary>
[Description("当InputType为数字类型时,能输入的最大值。")]
public decimal MaxValue
{
get
{
return this._maxValue;
}
set
{
this._maxValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,能输入的最小值。")]
public decimal MinValue
{
get
{
return this._minValue;
}
set
{
this._minValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,小数位数。")]
public int DecLength
{
get
{
return this._decLength;
}
set
{
this._decLength = value;
}
} public TextBoxEx()
{
this.InitializeComponent();
base.GotFocus += new EventHandler(this.TextBoxEx_GotFocus);
base.MouseUp += new MouseEventHandler(this.TextBoxEx_MouseUp);
base.KeyPress += TextBoxEx_KeyPress;
} void TextBoxEx_KeyPress(object sender, KeyPressEventArgs e)
{
//以下代码 取消按下回车或esc的“叮”声
if (e.KeyChar == System.Convert.ToChar() || e.KeyChar == System.Convert.ToChar())
{
e.Handled = true;
}
} private void TextBoxEx_MouseUp(object sender, MouseEventArgs e)
{
if (this.blnFocus)
{
base.SelectAll();
this.blnFocus = false;
}
} private void TextBoxEx_GotFocus(object sender, EventArgs e)
{
this.blnFocus = true;
base.SelectAll();
} private void TextBoxEx_TextChanged(object sender, EventArgs e)
{
if (this.Text == "")
{
this.m_strOldValue = this.Text;
}
else if (this.m_strOldValue != this.Text)
{
if (!ControlHelper.CheckInputType(this.Text, this._inputType, this._maxValue, this._minValue, this._decLength, this._regexPattern))
{
int num = base.SelectionStart;
if (this.m_strOldValue.Length < this.Text.Length)
{
num--;
}
else
{
num++;
}
base.TextChanged -= new EventHandler(this.TextBoxEx_TextChanged);
this.Text = this.m_strOldValue;
base.TextChanged += new EventHandler(this.TextBoxEx_TextChanged);
if (num < )
{
num = ;
}
base.SelectionStart = num;
}
else
{
this.m_strOldValue = this.Text;
}
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (string.IsNullOrEmpty(this.Text) && !string.IsNullOrEmpty(this._promptText))
{
if (e == null)
{
using (Graphics graphics = Graphics.FromHwnd(base.Handle))
{
if (this.Text.Length == && !string.IsNullOrEmpty(this.PromptText))
{
TextFormatFlags textFormatFlags = TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
if (this.RightToLeft == RightToLeft.Yes)
{
textFormatFlags |= (TextFormatFlags.Right | TextFormatFlags.RightToLeft);
}
TextRenderer.DrawText(graphics, this.PromptText, this._promptFont, base.ClientRectangle, this._promptColor, textFormatFlags);
}
}
}
}
} protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == || m.Msg == || m.Msg == )
{
this.OnPaint(null);
}
} protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
base.Invalidate();
}
}
}

用处及效果

用处:需要控制输入,需要显示水印

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

最新文章

  1. MVP 实例
  2. python核心编程学习记录之模块
  3. jquery 遍历 数组1
  4. linux下echo命令详解(转)
  5. sublime text3输入中文的问题.
  6. android的padding和margin的区别
  7. python数据的存储和持久化操作
  8. [Leetcode] implement strStr() (C++)
  9. 结构体的sizeof
  10. 学习笔记 css3--选择器&amp;新增颜色模式&amp;文本相关
  11. 用HTML5、地理定位API和Web服务来开发移动应用
  12. 利用margin代替小图标的绝对定位;使代码更简洁
  13. 在linux上创建nfs遇到的问题。
  14. c语言对齐问题
  15. linux 安装wordpress 无故往外发送大量垃圾邮件
  16. NB-IoT有三种部署方式及特点【转】
  17. nginx的with-http_sub_module模块使用之替换字符串
  18. ionic 视图滚动到顶部
  19. VS code 修改主题设置代码对其齐线
  20. Python 在序列上跟踪索引和值

热门文章

  1. DML语言DDL
  2. 个人永久性免费-Excel催化剂功能第29波-追加中国特色的中文相关自定义函数
  3. .NET Core 3.0之深入源码理解HttpClientFactory(一)
  4. David与Vincent的博弈游戏[树型DP]
  5. 自定义View之开关
  6. 从微信小程序开发者工具源码看实现原理(四)- - 自适应布局
  7. 解密Kafka吞吐量高的原因
  8. C#程序从Excel表格中读取数据并进行处理
  9. AIX7.1安装zabbix_agent3.4
  10. 温故而知新,重温 Java 7 的那些“新”特性