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

官網

http://www.hzhcontrols.comhtml

前提

入行已經7,8年了,一直想作一套漂亮點的自定義控件,因而就有了本系列文章。c++

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

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.gitgithub

若是以爲寫的還行,請點個 star 支持一下吧c#

歡迎前來交流探討: 企鵝羣568015492 企鵝羣568015492編輯器

目錄

http://www.javashuo.com/article/p-hacmmtru-mw.htmlide

準備工做

終於到文本框了,文本框將包含原文本框擴展,透明文本框,數字輸入文本框,帶邊框文本框字體

本文將講解數字輸入文本框,能夠經過加減按鈕來改變數字this

用到了無焦點窗體和鍵盤,若是你尚未了解,請前往查看spa

(十九)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         }

完整代碼

 1 // 版權全部 黃正輝 交流羣:568015492 QQ:623128629  2 // 文件名稱:UCNumTextBox.cs  3 // 建立日期:2019-08-15 16:03:54  4 // 功能描述:TextBox  5 // 項目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
 6 using System;  7 using System.Collections.Generic;  8 using System.ComponentModel;  9 using System.Drawing;  10 using System.Data;  11 using System.Linq;  12 using System.Text;  13 using System.Windows.Forms;  14 
 15 namespace HZH_Controls.Controls  16 {  17     [DefaultEvent("NumChanged")]  18     public partial class UCNumTextBox : UserControl  19  {  20         [Description("彈出輸入鍵盤時發生"), Category("自定義")]  21         public event EventHandler ShowKeyBorderEvent;  22         [Description("關閉輸入鍵盤時發生"), Category("自定義")]  23         public event EventHandler HideKeyBorderEvent;  24         [Description("數字改變時發生"), Category("自定義")]  25         public event EventHandler NumChanged;  26         /// <summary>
 27         /// 輸入類型  28         /// </summary>
 29         [Description("輸入類型"), Category("自定義")]  30         public TextInputType InputType  31  {  32             get
 33  {  34                 return txtNum.InputType;  35  }  36             set
 37  {  38                 if (value == TextInputType.NotControl)  39  {  40                     return;  41  }  42                 txtNum.InputType = value;  43  }  44  }  45 
 46         [Description("數字是否可手動編輯"), Category("自定義")]  47         public bool IsNumCanInput  48  {  49             get
 50  {  51                 return txtNum.Enabled;  52  }  53             set
 54  {  55                 txtNum.Enabled = value;  56  }  57  }  58         /// <summary>
 59         /// 當InputType爲數字類型時,能輸入的最大值  60         /// </summary>
 61         [Description("當InputType爲數字類型時,能輸入的最大值。")]  62         public decimal MaxValue  63  {  64             get
 65  {  66                 return this.txtNum.MaxValue;  67  }  68             set
 69  {  70                 this.txtNum.MaxValue = value;  71  }  72  }  73         /// <summary>
 74         /// 當InputType爲數字類型時,能輸入的最小值  75         /// </summary>
 76         [Description("當InputType爲數字類型時,能輸入的最小值。")]  77         public decimal MinValue  78  {  79             get
 80  {  81                 return this.txtNum.MinValue;  82  }  83             set
 84  {  85                 this.txtNum.MinValue = value;  86  }  87  }  88         private KeyBoardType keyBoardType = KeyBoardType.UCKeyBorderNum;  89         [Description("鍵盤樣式"), Category("自定義")]  90         public KeyBoardType KeyBoardType  91  {  92             get { return keyBoardType; }  93             set { keyBoardType = value; }  94  }  95 
 96         [Description("數值"), Category("自定義")]  97         public decimal Num  98  {  99             get { return txtNum.Text.ToDecimal(); } 100             set { txtNum.Text = value.ToString(); } 101  } 102         [Description("字體"), Category("自定義")] 103         public new Font Font 104  { 105             get
106  { 107                 return txtNum.Font; 108  } 109             set
110  { 111                 txtNum.Font = value; 112  } 113  } 114 
115         [Description("增長按鈕點擊事件"), Category("自定義")] 116         public event EventHandler AddClick; 117         [Description("減小按鈕點擊事件"), Category("自定義")] 118         public event EventHandler MinusClick; 119         public UCNumTextBox() 120  { 121  InitializeComponent(); 122             txtNum.TextChanged += txtNum_TextChanged; 123  } 124 
125         void txtNum_TextChanged(object sender, EventArgs e) 126  { 127             if (NumChanged != null) 128  { 129  NumChanged(txtNum.Text.ToString(), e); 130  } 131  } 132  Forms.FrmAnchor m_frmAnchor; 133         private void txtNum_MouseDown(object sender, MouseEventArgs e) 134  { 135             if (IsNumCanInput) 136  { 137                 if (KeyBoardType != HZH_Controls.Controls.KeyBoardType.Null) 138  { 139                     switch (keyBoardType) 140  { 141                         case KeyBoardType.UCKeyBorderAll_EN: 142 
143                             UCKeyBorderAll keyAll = new UCKeyBorderAll(); 144                             keyAll.RetractClike += (a, b) => { m_frmAnchor.Hide(); }; 145                             keyAll.EnterClick += (a, b) => { m_frmAnchor.Hide(); }; 146                             m_frmAnchor = new Forms.FrmAnchor(this, keyAll); 147                             m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged; 148 
149                             m_frmAnchor.Show(this.FindForm()); 150                             break; 151                         case KeyBoardType.UCKeyBorderNum: 152 
153                             UCKeyBorderNum keyNum = new UCKeyBorderNum(); 154                             keyNum.EnterClick += (a, b) => { m_frmAnchor.Hide(); }; 155                             m_frmAnchor = new Forms.FrmAnchor(this, keyNum); 156                             m_frmAnchor.VisibleChanged += m_frmAnchor_VisibleChanged; 157                             m_frmAnchor.Show(this.FindForm()); 158                             break; 159  } 160  } 161  } 162  } 163 
164         void m_frmAnchor_VisibleChanged(object sender, EventArgs e) 165  { 166             if (m_frmAnchor.Visible) 167  { 168                 if (ShowKeyBorderEvent != null) 169  { 170                     ShowKeyBorderEvent(this, null); 171  } 172  } 173             else
174  { 175                 if (HideKeyBorderEvent != null) 176  { 177                     HideKeyBorderEvent(this, null); 178  } 179  } 180  } 181 
182         public void NumAddClick() 183  { 184             btnAdd_MouseDown(null, null); 185  } 186 
187         public void NumMinusClick() 188  { 189             btnMinus_MouseDown(null, null); 190  } 191 
192         private void btnAdd_MouseDown(object sender, MouseEventArgs e) 193  { 194             if (AddClick != null) 195  { 196                 AddClick(this, e); 197  } 198             decimal dec = this.txtNum.Text.ToDecimal(); 199             dec++; 200             txtNum.Text = dec.ToString(); 201 
202  } 203 
204         private void btnMinus_MouseDown(object sender, MouseEventArgs e) 205  { 206             if (MinusClick != null) 207  { 208                 MinusClick(this, e); 209  } 210             decimal dec = this.txtNum.Text.ToDecimal(); 211             dec--; 212             txtNum.Text = dec.ToString(); 213  } 214 
215         private void UCNumTextBox_Load(object sender, EventArgs e) 216  { 217             this.txtNum.BackColor = this.BackColor; 218  } 219 
220         private void txtNum_FontChanged(object sender, EventArgs e) 221  { 222             txtNum.Location = new Point(txtNum.Location.X, (this.Height - txtNum.Height) / 2); 223  } 224 
225         private void UCNumTextBox_BackColorChanged(object sender, EventArgs e) 226  { 227             Color c = this.BackColor; 228             Control control = this; 229             while (c == Color.Transparent) 230  { 231                 control = control.Parent; 232                 if (control == null) 233                     break; 234                 c = control.BackColor; 235  } 236             if (c == Color.Transparent) 237                 return; 238             txtNum.BackColor = c; 239  } 240  } 241 }
View Code
 1 namespace HZH_Controls.Controls  2 {  3     partial class UCNumTextBox  4  {  5         /// <summary> 
 6         /// 必需的設計器變量。  7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;  9 
 10         /// <summary> 
 11         /// 清理全部正在使用的資源。  12         /// </summary>
 13         /// <param name="disposing">若是應釋放託管資源,爲 true;不然爲 false。</param>
 14         protected override void Dispose(bool disposing)  15  {  16             if (disposing && (components != null))  17  {  18  components.Dispose();  19  }  20             base.Dispose(disposing);  21  }  22 
 23         #region 組件設計器生成的代碼
 24 
 25         /// <summary> 
 26         /// 設計器支持所需的方法 - 不要  27         /// 使用代碼編輯器修改此方法的內容。  28         /// </summary>
 29         private void InitializeComponent()  30  {  31             this.txtNum = new HZH_Controls.Controls.TextBoxEx();  32             this.btnMinus = new System.Windows.Forms.Panel();  33             this.btnAdd = new System.Windows.Forms.Panel();  34             this.SuspendLayout();  35             // 
 36             // txtNum  37             // 
 38             this.txtNum.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));  39             this.txtNum.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));  40             this.txtNum.BorderStyle = System.Windows.Forms.BorderStyle.None;  41             this.txtNum.DecLength = 3;  42             this.txtNum.Font = new System.Drawing.Font("Arial Unicode MS", 15F);  43             this.txtNum.InputType = TextInputType.Number;  44             this.txtNum.Location = new System.Drawing.Point(37, 11);  45             this.txtNum.Margin = new System.Windows.Forms.Padding(0);  46             this.txtNum.MaxValue = new decimal(new int[] {  47             1000000,  48             0,  49             0,  50             0});  51             this.txtNum.MinValue = new decimal(new int[] {  52             0,  53             0,  54             0,  55             0});  56             this.txtNum.MyRectangle = new System.Drawing.Rectangle(0, 0, 0, 0);  57             this.txtNum.Name = "txtNum";  58             this.txtNum.OldText = null;  59             this.txtNum.PromptColor = System.Drawing.Color.Gray;  60             this.txtNum.PromptFont = new System.Drawing.Font("微軟雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);  61             this.txtNum.PromptText = "";  62             this.txtNum.RegexPattern = "";  63             this.txtNum.Size = new System.Drawing.Size(78, 27);  64             this.txtNum.TabIndex = 9;  65             this.txtNum.Text = "1";  66             this.txtNum.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;  67             this.txtNum.FontChanged += new System.EventHandler(this.txtNum_FontChanged);  68             this.txtNum.MouseDown += new System.Windows.Forms.MouseEventHandler(this.txtNum_MouseDown);  69             // 
 70             // btnMinus  71             // 
 72             this.btnMinus.BackColor = System.Drawing.Color.Transparent;  73             this.btnMinus.BackgroundImage = global::HZH_Controls.Properties.Resources.ic_remove_black_18dp;  74             this.btnMinus.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;  75             this.btnMinus.Dock = System.Windows.Forms.DockStyle.Left;  76             this.btnMinus.Location = new System.Drawing.Point(2, 2);  77             this.btnMinus.Name = "btnMinus";  78             this.btnMinus.Size = new System.Drawing.Size(32, 40);  79             this.btnMinus.TabIndex = 6;  80             this.btnMinus.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnMinus_MouseDown);  81             // 
 82             // btnAdd  83             // 
 84             this.btnAdd.BackColor = System.Drawing.Color.Transparent;  85             this.btnAdd.BackgroundImage = global::HZH_Controls.Properties.Resources.ic_add_black_18dp;  86             this.btnAdd.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;  87             this.btnAdd.Dock = System.Windows.Forms.DockStyle.Right;  88             this.btnAdd.Location = new System.Drawing.Point(118, 2);  89             this.btnAdd.Name = "btnAdd";  90             this.btnAdd.Size = new System.Drawing.Size(32, 40);  91             this.btnAdd.TabIndex = 5;  92             this.btnAdd.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnAdd_MouseDown);  93             // 
 94             // UCNumTextBox  95             // 
 96             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;  97             this.Controls.Add(this.txtNum);  98             this.Controls.Add(this.btnMinus);  99             this.Controls.Add(this.btnAdd); 100             this.Name = "UCNumTextBox"; 101             this.Padding = new System.Windows.Forms.Padding(2); 102             this.Size = new System.Drawing.Size(152, 44); 103             this.Load += new System.EventHandler(this.UCNumTextBox_Load); 104             this.BackColorChanged += new System.EventHandler(this.UCNumTextBox_BackColorChanged); 105             this.ResumeLayout(false); 106             this.PerformLayout(); 107 
108  } 109 
110         #endregion
111 
112         private System.Windows.Forms.Panel btnAdd; 113         private System.Windows.Forms.Panel btnMinus; 114         private TextBoxEx txtNum; 115  } 116 }
View Code

設計效果

 

用處及效果

 

最後的話

若是你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星 星吧

相關文章
相關標籤/搜索