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

前提

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

官网:https://www.hzhcontrols.cn

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

码云:HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~

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

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

目录

c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-CSDN博客

准备工作

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

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

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

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

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

开始

添加用户控件,命名UCNumTextBox

有这些属性

复制代码

 1  [Description("弹出输入键盘时发生"), Category("自定义")]
 2         public event EventHandler ShowKeyBorderEvent;
 3         [Description("关闭输入键盘时发生"), Category("自定义")]
 4         public event EventHandler HideKeyBorderEvent;
 5         [Description("数字改变时发生"), Category("自定义")]
 6         public event EventHandler NumChanged;
 7         /// <summary>
 8         /// 输入类型
 9         /// </summary>
10         [Description("输入类型"), Category("自定义")]
11         public TextInputType InputType
12         {
13             get
14             {
15                 return txtNum.InputType;
16             }
17             set
18             {
19                 if (value == TextInputType.NotControl)
20                 {
21                     return;
22                 }
23                 txtNum.InputType = value;
24             }
25         }
26 
27         [Description("数字是否可手动编辑"), Category("自定义")]
28         public bool IsNumCanInput
29         {
30             get
31             {
32                 return txtNum.Enabled;
33             }
34             set
35             {
36                 txtNum.Enabled = value;
37             }
38         }
39         /// <summary>
40         /// 当InputType为数字类型时,能输入的最大值
41         /// </summary>
42         [Description("当InputType为数字类型时,能输入的最大值。")]
43         public decimal MaxValue
44         {
45             get
46             {
47                 return this.txtNum.MaxValue;
48             }
49             set
50             {
51                 this.txtNum.MaxValue = value;
52             }
53         }
54         /// <summary>
55         /// 当InputType为数字类型时,能输入的最小值
56         /// </summary>
57         [Description("当InputType为数字类型时,能输入的最小值。")]
58         public decimal MinValue
59         {
60             get
61             {
62                 return this.txtNum.MinValue;
63             }
64             set
65             {
66                 this.txtNum.MinValue = value;
67             }
68         }
69         private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderNum;
70         [Description("键盘样式"), Category("自定义")]
71         public KeyBoardType KeyBoardType
72         {
73             get { return keyBoardType; }
74             set { keyBoardType = value; }
75         }
76 
77         [Description("数值"), Category("自定义")]
78         public decimal Num
79         {
80             get { return txtNum.Text.ToDecimal(); }
81             set { txtNum.Text = value.ToString(); }
82         }
83         [Description("字体"), Category("自定义")]
84         public new Font Font
85         {
86             get
87             {
88                 return txtNum.Font;
89             }
90             set
91             {
92                 txtNum.Font = value;
93             }
94         }
95 
96         [Description("增加按钮点击事件"), Category("自定义")]
97         public event EventHandler AddClick;
98         [Description("减少按钮点击事件"), Category("自定义")]
99         public event EventHandler MinusClick;

复制代码

一些事件

复制代码

  1  void txtNum_TextChanged(object sender, EventArgs e)
  2         {
  3             if (NumChanged != null)
  4             {
  5                 NumChanged(txtNum.Text.ToString(), e);
  6             }
  7         }
  8         Forms.FrmAnchor m_frmAnchor;
  9         private void txtNum_MouseDown(object sender, MouseEventArgs e)
 10         {
 11             if (IsNumCanInput)
 12             {
 13                 if (KeyBoardType != HZH_Controls.Controls.KeyBoardType.Null)
 14                 {
 15                     switch (keyBoardType)
 16                     {
 17                         case KeyBoardType.UCKeyBorderAll_EN:
 18 
 19                             UCKeyBorderAll keyAll = new UCKeyBorderAll();
 20                             keyAll.RetractClike += (a, b) => { m_frmAnchor.Hide(); };
 21                             keyAll.EnterClick += (a, b) => { m_frmAnchor.Hide(); };
 22                             m_frmAnchor = new Forms.FrmAnchor(this, keyAll);
 23                             m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged;
 24 
 25                             m_frmAnchor.Show(this.FindForm());
 26                             break;
 27                         case KeyBoardType.UCKeyBorderNum:
 28 
 29                             UCKeyBorderNum keyNum = new UCKeyBorderNum();
 30                             keyNum.EnterClick += (a, b) => { m_frmAnchor.Hide(); };
 31                             m_frmAnchor = new Forms.FrmAnchor(this, keyNum);
 32                             m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged;
 33                             m_frmAnchor.Show(this.FindForm());
 34                             break;
 35                     }
 36                 }
 37             }
 38         }
 39 
 40         void m_frmAnchor_VisibleChanged(object sender, EventArgs e)
 41         {
 42             if (m_frmAnchor.Visible)
 43             {
 44                 if (ShowKeyBorderEvent != null)
 45                 {
 46                     ShowKeyBorderEvent(this, null);
 47                 }
 48             }
 49             else
 50             {
 51                 if (HideKeyBorderEvent != null)
 52                 {
 53                     HideKeyBorderEvent(this, null);
 54                 }
 55             }
 56         }
 57 
 58         public void NumAddClick()
 59         {
 60             btnAdd_MouseDown(null, null);
 61         }
 62 
 63         public void NumMinusClick()
 64         {
 65             btnMinus_MouseDown(null, null);
 66         }
 67 
 68         private void btnAdd_MouseDown(object sender, MouseEventArgs e)
 69         {
 70             if (AddClick != null)
 71             {
 72                 AddClick(this, e);
 73             }
 74             decimal dec = this.txtNum.Text.ToDecimal();
 75             dec++;
 76             txtNum.Text = dec.ToString();
 77 
 78         }
 79 
 80         private void btnMinus_MouseDown(object sender, MouseEventArgs e)
 81         {
 82             if (MinusClick != null)
 83             {
 84                 MinusClick(this, e);
 85             }
 86             decimal dec = this.txtNum.Text.ToDecimal();
 87             dec--;
 88             txtNum.Text = dec.ToString();
 89         }
 90 
 91         private void UCNumTextBox_Load(object sender, EventArgs e)
 92         {
 93             this.txtNum.BackColor = this.BackColor;
 94         }
 95 
 96         private void txtNum_FontChanged(object sender, EventArgs e)
 97         {
 98             txtNum.Location = new Point(txtNum.Location.X, (this.Height - txtNum.Height) / 2);
 99         }
100 
101         private void UCNumTextBox_BackColorChanged(object sender, EventArgs e)
102         {
103             Color c = this.BackColor;
104             Control control = this;
105             while (c == Color.Transparent)
106             {
107                 control = control.Parent;
108                 if (control == null)
109                     break;
110                 c = control.BackColor;
111             }
112             if (c == Color.Transparent)
113                 return;
114             txtNum.BackColor = c;
115         }

复制代码

完整代码

// 版权所有  黄正辉  交流群: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) / 2);
        }

        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)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
            this.txtNum.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtNum.DecLength = 3;
            this.txtNum.Font = new System.Drawing.Font("Arial Unicode MS", 15F);
            this.txtNum.InputType = TextInputType.Number;
            this.txtNum.Location = new System.Drawing.Point(37, 11);
            this.txtNum.Margin = new System.Windows.Forms.Padding(0);
            this.txtNum.MaxValue = new decimal(new int[] {
            1000000,
            0,
            0,
            0});
            this.txtNum.MinValue = new decimal(new int[] {
            0,
            0,
            0,
            0});
            this.txtNum.MyRectangle = new System.Drawing.Rectangle(0, 0, 0, 0);
            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(78, 27);
            this.txtNum.TabIndex = 9;
            this.txtNum.Text = "1";
            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(2, 2);
            this.btnMinus.Name = "btnMinus";
            this.btnMinus.Size = new System.Drawing.Size(32, 40);
            this.btnMinus.TabIndex = 6;
            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(118, 2);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(32, 40);
            this.btnAdd.TabIndex = 5;
            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(2);
            this.Size = new System.Drawing.Size(152, 44);
            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;
    }
}

设计效果

用处及效果

最后的话

如果你喜欢的话,请到 HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~ 点个星 星吧

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值