官网

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

准备工作

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

本文将讲解数字输入文本框,可以通过加减按钮来改变数字

用到了无焦点窗体和键盘,如果你还没有了解,请前往查看

(十九)c#Winform自定义控件-停靠窗体

(十五)c#Winform自定义控件-键盘(二)

开始

添加用户控件,命名UCNumTextBox

有这些属性

  [Description("弹出输入键盘时发生"), Category("自定义")]
public event EventHandler ShowKeyBorderEvent;
[Description("关闭输入键盘时发生"), Category("自定义")]
public event EventHandler HideKeyBorderEvent;
[Description("数字改变时发生"), Category("自定义")]
public event EventHandler NumChanged;
/// <summary>
/// 输入类型
/// </summary>
[Description("输入类型"), Category("自定义")]
public TextInputType InputType
{
get
{
return txtNum.InputType;
}
set
{
if (value == TextInputType.NotControl)
{
return;
}
txtNum.InputType = value;
}
} [Description("数字是否可手动编辑"), Category("自定义")]
public bool IsNumCanInput
{
get
{
return txtNum.Enabled;
}
set
{
txtNum.Enabled = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最大值
/// </summary>
[Description("当InputType为数字类型时,能输入的最大值。")]
public decimal MaxValue
{
get
{
return this.txtNum.MaxValue;
}
set
{
this.txtNum.MaxValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,能输入的最小值。")]
public decimal MinValue
{
get
{
return this.txtNum.MinValue;
}
set
{
this.txtNum.MinValue = value;
}
}
private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderNum;
[Description("键盘样式"), Category("自定义")]
public KeyBoardType KeyBoardType
{
get { return keyBoardType; }
set { keyBoardType = value; }
} [Description("数值"), Category("自定义")]
public decimal Num
{
get { return txtNum.Text.ToDecimal(); }
set { txtNum.Text = value.ToString(); }
}
[Description("字体"), Category("自定义")]
public new Font Font
{
get
{
return txtNum.Font;
}
set
{
txtNum.Font = value;
}
} [Description("增加按钮点击事件"), Category("自定义")]
public event EventHandler AddClick;
[Description("减少按钮点击事件"), Category("自定义")]
public event EventHandler MinusClick;

一些事件

  void txtNum_TextChanged(object sender, EventArgs e)
{
if (NumChanged != null)
{
NumChanged(txtNum.Text.ToString(), e);
}
}
Forms.FrmAnchor m_frmAnchor;
private void txtNum_MouseDown(object sender, MouseEventArgs e)
{
if (IsNumCanInput)
{
if (KeyBoardType != HZH_Controls.Controls.KeyBoardType.Null)
{
switch (keyBoardType)
{
case KeyBoardType.UCKeyBorderAll_EN: UCKeyBorderAll keyAll = new UCKeyBorderAll();
keyAll.RetractClike += (a, b) => { m_frmAnchor.Hide(); };
keyAll.EnterClick += (a, b) => { m_frmAnchor.Hide(); };
m_frmAnchor = new Forms.FrmAnchor(this, keyAll);
m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged; m_frmAnchor.Show(this.FindForm());
break;
case KeyBoardType.UCKeyBorderNum: UCKeyBorderNum keyNum = new UCKeyBorderNum();
keyNum.EnterClick += (a, b) => { m_frmAnchor.Hide(); };
m_frmAnchor = new Forms.FrmAnchor(this, keyNum);
m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged;
m_frmAnchor.Show(this.FindForm());
break;
}
}
}
} void m_frmAnchor_VisibleChanged(object sender, EventArgs e)
{
if (m_frmAnchor.Visible)
{
if (ShowKeyBorderEvent != null)
{
ShowKeyBorderEvent(this, null);
}
}
else
{
if (HideKeyBorderEvent != null)
{
HideKeyBorderEvent(this, null);
}
}
} public void NumAddClick()
{
btnAdd_MouseDown(null, null);
} public void NumMinusClick()
{
btnMinus_MouseDown(null, null);
} private void btnAdd_MouseDown(object sender, MouseEventArgs e)
{
if (AddClick != null)
{
AddClick(this, e);
}
decimal dec = this.txtNum.Text.ToDecimal();
dec++;
txtNum.Text = dec.ToString(); } private void btnMinus_MouseDown(object sender, MouseEventArgs e)
{
if (MinusClick != null)
{
MinusClick(this, e);
}
decimal dec = this.txtNum.Text.ToDecimal();
dec--;
txtNum.Text = dec.ToString();
} private void UCNumTextBox_Load(object sender, EventArgs e)
{
this.txtNum.BackColor = this.BackColor;
} private void txtNum_FontChanged(object sender, EventArgs e)
{
txtNum.Location = new Point(txtNum.Location.X, (this.Height - txtNum.Height) / );
} private void UCNumTextBox_BackColorChanged(object sender, EventArgs e)
{
Color c = this.BackColor;
Control control = this;
while (c == Color.Transparent)
{
control = control.Parent;
if (control == null)
break;
c = control.BackColor;
}
if (c == Color.Transparent)
return;
txtNum.BackColor = c;
}

完整代码

 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCNumTextBox.cs
// 创建日期:2019-08-15 16:03:54
// 功能描述:TextBox
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace HZH_Controls.Controls
{
[DefaultEvent("NumChanged")]
public partial class UCNumTextBox : UserControl
{
[Description("弹出输入键盘时发生"), Category("自定义")]
public event EventHandler ShowKeyBorderEvent;
[Description("关闭输入键盘时发生"), Category("自定义")]
public event EventHandler HideKeyBorderEvent;
[Description("数字改变时发生"), Category("自定义")]
public event EventHandler NumChanged;
/// <summary>
/// 输入类型
/// </summary>
[Description("输入类型"), Category("自定义")]
public TextInputType InputType
{
get
{
return txtNum.InputType;
}
set
{
if (value == TextInputType.NotControl)
{
return;
}
txtNum.InputType = value;
}
} [Description("数字是否可手动编辑"), Category("自定义")]
public bool IsNumCanInput
{
get
{
return txtNum.Enabled;
}
set
{
txtNum.Enabled = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最大值
/// </summary>
[Description("当InputType为数字类型时,能输入的最大值。")]
public decimal MaxValue
{
get
{
return this.txtNum.MaxValue;
}
set
{
this.txtNum.MaxValue = value;
}
}
/// <summary>
/// 当InputType为数字类型时,能输入的最小值
/// </summary>
[Description("当InputType为数字类型时,能输入的最小值。")]
public decimal MinValue
{
get
{
return this.txtNum.MinValue;
}
set
{
this.txtNum.MinValue = value;
}
}
private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderNum;
[Description("键盘样式"), Category("自定义")]
public KeyBoardType KeyBoardType
{
get { return keyBoardType; }
set { keyBoardType = value; }
} [Description("数值"), Category("自定义")]
public decimal Num
{
get { return txtNum.Text.ToDecimal(); }
set { txtNum.Text = value.ToString(); }
}
[Description("字体"), Category("自定义")]
public new Font Font
{
get
{
return txtNum.Font;
}
set
{
txtNum.Font = value;
}
} [Description("增加按钮点击事件"), Category("自定义")]
public event EventHandler AddClick;
[Description("减少按钮点击事件"), Category("自定义")]
public event EventHandler MinusClick;
public UCNumTextBox()
{
InitializeComponent();
txtNum.TextChanged += txtNum_TextChanged;
} void txtNum_TextChanged(object sender, EventArgs e)
{
if (NumChanged != null)
{
NumChanged(txtNum.Text.ToString(), e);
}
}
Forms.FrmAnchor m_frmAnchor;
private void txtNum_MouseDown(object sender, MouseEventArgs e)
{
if (IsNumCanInput)
{
if (KeyBoardType != HZH_Controls.Controls.KeyBoardType.Null)
{
switch (keyBoardType)
{
case KeyBoardType.UCKeyBorderAll_EN: UCKeyBorderAll keyAll = new UCKeyBorderAll();
keyAll.RetractClike += (a, b) => { m_frmAnchor.Hide(); };
keyAll.EnterClick += (a, b) => { m_frmAnchor.Hide(); };
m_frmAnchor = new Forms.FrmAnchor(this, keyAll);
m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged; m_frmAnchor.Show(this.FindForm());
break;
case KeyBoardType.UCKeyBorderNum: UCKeyBorderNum keyNum = new UCKeyBorderNum();
keyNum.EnterClick += (a, b) => { m_frmAnchor.Hide(); };
m_frmAnchor = new Forms.FrmAnchor(this, keyNum);
m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged;
m_frmAnchor.Show(this.FindForm());
break;
}
}
}
} void m_frmAnchor_VisibleChanged(object sender, EventArgs e)
{
if (m_frmAnchor.Visible)
{
if (ShowKeyBorderEvent != null)
{
ShowKeyBorderEvent(this, null);
}
}
else
{
if (HideKeyBorderEvent != null)
{
HideKeyBorderEvent(this, null);
}
}
} public void NumAddClick()
{
btnAdd_MouseDown(null, null);
} public void NumMinusClick()
{
btnMinus_MouseDown(null, null);
} private void btnAdd_MouseDown(object sender, MouseEventArgs e)
{
if (AddClick != null)
{
AddClick(this, e);
}
decimal dec = this.txtNum.Text.ToDecimal();
dec++;
txtNum.Text = dec.ToString(); } private void btnMinus_MouseDown(object sender, MouseEventArgs e)
{
if (MinusClick != null)
{
MinusClick(this, e);
}
decimal dec = this.txtNum.Text.ToDecimal();
dec--;
txtNum.Text = dec.ToString();
} private void UCNumTextBox_Load(object sender, EventArgs e)
{
this.txtNum.BackColor = this.BackColor;
} private void txtNum_FontChanged(object sender, EventArgs e)
{
txtNum.Location = new Point(txtNum.Location.X, (this.Height - txtNum.Height) / );
} private void UCNumTextBox_BackColorChanged(object sender, EventArgs e)
{
Color c = this.BackColor;
Control control = this;
while (c == Color.Transparent)
{
control = control.Parent;
if (control == null)
break;
c = control.BackColor;
}
if (c == Color.Transparent)
return;
txtNum.BackColor = c;
}
}
}
 namespace HZH_Controls.Controls
{
partial class UCNumTextBox
{
/// <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.txtNum = new HZH_Controls.Controls.TextBoxEx();
this.btnMinus = new System.Windows.Forms.Panel();
this.btnAdd = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// txtNum
//
this.txtNum.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.txtNum.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.txtNum.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtNum.DecLength = ;
this.txtNum.Font = new System.Drawing.Font("Arial Unicode MS", 15F);
this.txtNum.InputType = TextInputType.Number;
this.txtNum.Location = new System.Drawing.Point(, );
this.txtNum.Margin = new System.Windows.Forms.Padding();
this.txtNum.MaxValue = new decimal(new int[] {
,
,
,
});
this.txtNum.MinValue = new decimal(new int[] {
,
,
,
});
this.txtNum.MyRectangle = new System.Drawing.Rectangle(, , , );
this.txtNum.Name = "txtNum";
this.txtNum.OldText = null;
this.txtNum.PromptColor = System.Drawing.Color.Gray;
this.txtNum.PromptFont = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
this.txtNum.PromptText = "";
this.txtNum.RegexPattern = "";
this.txtNum.Size = new System.Drawing.Size(, );
this.txtNum.TabIndex = ;
this.txtNum.Text = "";
this.txtNum.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.txtNum.FontChanged += new System.EventHandler(this.txtNum_FontChanged);
this.txtNum.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txtNum_MouseDown);
//
// btnMinus
//
this.btnMinus.BackColor = System.Drawing.Color.Transparent;
this.btnMinus.BackgroundImage = global::HZH_Controls.Properties.Resources.ic_remove_black_18dp;
this.btnMinus.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.btnMinus.Dock = System.Windows.Forms.DockStyle.Left;
this.btnMinus.Location = new System.Drawing.Point(, );
this.btnMinus.Name = "btnMinus";
this.btnMinus.Size = new System.Drawing.Size(, );
this.btnMinus.TabIndex = ;
this.btnMinus.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnMinus_MouseDown);
//
// btnAdd
//
this.btnAdd.BackColor = System.Drawing.Color.Transparent;
this.btnAdd.BackgroundImage = global::HZH_Controls.Properties.Resources.ic_add_black_18dp;
this.btnAdd.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.btnAdd.Dock = System.Windows.Forms.DockStyle.Right;
this.btnAdd.Location = new System.Drawing.Point(, );
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(, );
this.btnAdd.TabIndex = ;
this.btnAdd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnAdd_MouseDown);
//
// UCNumTextBox
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Controls.Add(this.txtNum);
this.Controls.Add(this.btnMinus);
this.Controls.Add(this.btnAdd);
this.Name = "UCNumTextBox";
this.Padding = new System.Windows.Forms.Padding();
this.Size = new System.Drawing.Size(, );
this.Load += new System.EventHandler(this.UCNumTextBox_Load);
this.BackColorChanged += new System.EventHandler(this.UCNumTextBox_BackColorChanged);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Panel btnAdd;
private System.Windows.Forms.Panel btnMinus;
private TextBoxEx txtNum;
}
}

设计效果

用处及效果

最后的话

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

(三十)c#Winform自定义控件-文本框(三)的更多相关文章

  1. (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  2. (三十一)c#Winform自定义控件-文本框(四)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (二十八)c#Winform自定义控件-文本框(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. (二十九)c#Winform自定义控件-文本框(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  5. (八十二)c#Winform自定义控件-穿梭框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  6. (十八)c#Winform自定义控件-提示框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. (三)c#Winform自定义控件-有图标的按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. 邁向IT專家成功之路的三十則鐵律 鐵律三十 IT人成功之道-總結

    時間過得相當快!很榮幸有這個機會能夠在iT邦幫忙鐵人賽的社群網站上,和所有IT人分享「邁向IT專家成功之路的三十則鐵律」,其主要目的只是單純希望每一位辛苦的IT工作者,盡可能可以從這一些經驗分享中,將 ...

  9. 程序员与年龄:四十岁普通开发、三十五岁首席架构、三十岁基层Leader

    最近,有一个词儿特别热门--躺平.有没有人跟你说过:"躺平说起来容易,做起来更容易." 和躺平相对的是另外一个词--内卷,群聊的时候,已经很多次看过草卷起来了.jpg表情包.某些节 ...

随机推荐

  1. Mybatis辅助神器-MyBatis Log Plugin

    1. 问题描述 Java操作数据库的两台流行天王-mybatis和hibernate,mytabis和hibernate的区别不想废话了,以前用hibernate,最近几年一直用的mybatis,目前 ...

  2. bzoj3316 JC loves Mkk题解

    3316: JC loves Mkk Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 979  Solved: 316[Submit][Status][Di ...

  3. Windows OS添加USB3.0驱动!

    原因:现在的win7.win2008等一些早期的系统在安装时无法使USB鼠标和键盘... 1.  素材(dism64工具.UltraISO工具.U盘一个) 2.  先使用UltrISO工具制作一个wi ...

  4. C# 中异常抛出捕获机制--throw / try,catch,finally

    try { messagebox.show("true"); } catch { messagebox.show("false"); } finally { m ...

  5. 爬虫之突破xm-sign校验反爬

    喜马拉雅 网页分析 - 打开我们要爬取的音乐专辑https://www.ximalaya.com/ertong/424529/ - F12打开开发者工具 - 点击XHR 随便点击一首歌曲会看到存储所有 ...

  6. Python入门基础(8)

    上一篇介绍了一些python中函数的基本用法,那么,现在就来谈一谈python中的一些内置函数吧 python中的内置函数 1.map()函数 map()函数接受两个参数值,一个是函数,一个是Iter ...

  7. weex起步

    weex文档地址: http://weex-project.io/cn/guide/index.html weex的文档过于简单,加上js语法 & android & ios本身也有很 ...

  8. java-org.springframework.core.convert.ConversionFailedException- 前端传string解析date异常

    关于SpringMVC前台日期作为实体类对象参数类型转换错误解决 异常信息: Field error in object 'tblHouse' on field 'houseTime': reject ...

  9. 原 docker 安装使用 solr

    1.安装solr 7.5 docker solr 官网:https://hub.docker.com/_/solr/ docker pull solr:7.5.0 2.启动solr服务 docker ...

  10. typedef int a[10];怎么解释?

    typedef int a[10]; a b[10]; 为什么分配400个字节的空间? int a[10];为什么分配了40个字节的空间? 问题:应该怎么解释typedef的这种行为呢?而如果换成是# ...