(二十八)c#Winform自定义控件-文本框(一)
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
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 点个星 星吧
(二十八)c#Winform自定义控件-文本框(一)的更多相关文章
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (二十九)c#Winform自定义控件-文本框(二)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十一)c#Winform自定义控件-文本框(四)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (二十)c#Winform自定义控件-有后退的窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- NLP(二十八)多标签文本分类
本文将会讲述如何实现多标签文本分类. 什么是多标签分类? 在分类问题中,我们已经接触过二分类和多分类问题了.所谓二(多)分类问题,指的是y值一共有两(多)个类别,每个样本的y值只能属于其中的一 ...
- (转载)Android项目实战(二十八):使用Zxing实现二维码及优化实例
Android项目实战(二十八):使用Zxing实现二维码及优化实例 作者:听着music睡 字体:[增加 减小] 类型:转载 时间:2016-11-21我要评论 这篇文章主要介绍了Android项目 ...
- VMware vSphere 服务器虚拟化之二十八 桌面虚拟化之安装View传输服务器
VMware vSphere 服务器虚拟化之二十八 桌面虚拟化之安装View传输服务器 View 传输服务器用于管理和简化数据中心与在最终用户本地系统上检出使用的 View 桌面之间的数据传输.必须安 ...
- (转载)Android项目实战(二十八):Zxing二维码实现及优化
Android项目实战(二十八):Zxing二维码实现及优化 前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中 ...
随机推荐
- [开源]OSharpNS 步步为营系列 - 3. 添加业务服务层
什么是OSharp OSharpNS全称OSharp Framework with .NetStandard2.0,是一个基于.NetStandard2.0开发的一个.NetCore快速开发框架.这个 ...
- Centos7:yum安装MySQL5.7后如何设置root密码
Centos下安装软件的方式很简单,只需要通过yum install xxx命令即可.第一步当然检查是否有mysql的yum源,命令:yum list|grep mysql-community[主要还 ...
- Python一秒提供Rest接口
Python一秒提供Rest接口 使用的是Anaconda安装的Python环境; 新建py文件(例如:restapi.py) # -*- coding: utf-8 -*- from flask i ...
- Bzoj 2281 [Sdoi2011]黑白棋 题解
2281: [Sdoi2011]黑白棋 Time Limit: 3 Sec Memory Limit: 512 MBSubmit: 592 Solved: 362[Submit][Status][ ...
- ServiceFabric极简文档-2 部署环境搭建-配置文件
类型:ClusterConfig.Unsecure.MultiMachine 说明:至少3台机子 { "name": "SampleCluster", &quo ...
- GPS常识-B版(简)
第一章 绪论 1.简述GPS系统的特点有哪些? 在测绘工程中有如下优点:(1)定位精度高(2)观测时间短(3)测站间无需通视(4)可提供地心坐标(5)操作简便(6)全天候作业(7)功能多.应用广 GP ...
- 分享基于EF6、Unitwork、Autofac的Repository模式设计
目录 分享基于EF6.Unitwork.Autofac的Repository模式设计 一.实现的思路和结构图 二.Repository设计具体的实现代码 三.Repository设计的具体的使用 四. ...
- jenkins +Jmeter 完成分布式性能测试
1.Jmeter 压测机器配置. 下载Jmeter 版本:https://jmeter.apache.org/download_jmeter.cgi 我下的是5.1.1 将下载后的版本进行解压. ...
- ASP.NET Core MVC 之视图(Views)
ASP.NET Core MVC 控制器可以使用视图返回格式化的结果. 1.什么是视图 在 MVC 中,视图封装了用户与应用交互呈现细节.视图是具有生成要发送到客户端内容的,包含嵌入代码的HTML模板 ...
- 《ElasticSearch6.x实战教程》之实战ELK日志分析系统、多数据源同步
第十章-实战:ELK日志分析系统 ElasticSearch.Logstash.Kibana简称ELK系统,主要用于日志的收集与分析. 一个完整的大型分布式系统,会有很多与业务不相关的系统,其中日志系 ...