官网

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">(三十)c#Winform自定义控件-文本框(三)-LMLPHP

目录

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;
}
}

设计效果

(三十)c#Winform自定义控件-文本框(三)-LMLPHP

用处及效果

(三十)c#Winform自定义控件-文本框(三)-LMLPHP

最后的话

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

05-11 15:26