http://www.hzhcontrols.com

前提

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

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

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

欢迎前来交流探讨: 企鹅群568015492 (十四)c#Winform自定义控件-键盘(一)-HZHControls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

键盘控件目前分为4中,英文键盘,数字键盘,支付键盘,手写键盘

键盘一般用在到文本框弹出的键盘,那么为什么到现在还没有看到文本框的影子呢?因为文本框的某些功能牵扯到了自定义窗体,所以准备在自定义窗体介绍之后再来说文本框。

本篇文章介绍英文键盘

开始

添加用户控件,命名UCKeyBorderAll

定义枚举,显示模式

1 public enum KeyBorderCharType
2     {
3         CHAR = 1,
4         NUMBER = 2
5     }

属性

 1  private KeyBorderCharType _charType = KeyBorderCharType.CHAR;
 2 
 3         [Description("默认显示样式"), Category("自定义")]
 4         public KeyBorderCharType CharType
 5         {
 6             get { return _charType; }
 7             set
 8             {
 9                 _charType = value;
10                 if (value == KeyBorderCharType.CHAR)
11                 {
12                     if (label37.Text.ToLower() == "abc.")
13                     {
14                         CharOrNum();
15                     }
16                 }
17                 else
18                 {
19                     if (label37.Text.ToLower() == "?123")
20                     {
21                         CharOrNum();
22                     }
23                 }
24             }
25         }
26         [Description("按键点击事件"), Category("自定义")]
27         public event EventHandler KeyClick;
28         [Description("回车点击事件"), Category("自定义")]
29         public event EventHandler EnterClick;
30         [Description("删除点击事件"), Category("自定义")]
31         public event EventHandler BackspaceClike;
32         [Description("收起点击事件"), Category("自定义")]
33         public event EventHandler RetractClike;

按钮事件

 1 private void KeyDown_MouseDown(object sender, MouseEventArgs e)
 2         {
 3             Label lbl = sender as Label;
 4             if (string.IsNullOrEmpty(lbl.Text))
 5             {
 6                 return;
 7             }
 8             if (lbl.Text == "大写")
 9             {
10                 ToUper(true);
11                 lbl.Text = "小写";
12             }
13             else if (lbl.Text == "小写")
14             {
15                 ToUper(false);
16                 lbl.Text = "大写";
17             }
18             else if (lbl.Text == "?123" || lbl.Text == "abc.")
19             {
20                 CharOrNum();
21             }
22             else if (lbl.Text == "空格")
23             {
24                 SendKeys.Send(" ");
25             }
26             else if (lbl.Text == "删除")
27             {
28                 SendKeys.Send("{BACKSPACE}");
29                 if (BackspaceClike != null)
30                     BackspaceClike(sender, e);
31             }
32             else if (lbl.Text == "回车")
33             {
34                 SendKeys.Send("{ENTER}");
35                 if (EnterClick != null)
36                     EnterClick(sender, e);
37             }
38             else if (lbl.Text == "收起")
39             {
40                 if (RetractClike != null)
41                     RetractClike(sender, e);
42             }
43             else
44             {
45                 SendKeys.Send(lbl.Text);
46             }
47             if (KeyClick != null)
48                 KeyClick(sender, e);
49         }

辅助函数

 1 private void ToUper(bool bln)
 2         {
 3             foreach (Control item in this.tableLayoutPanel2.Controls)
 4             {
 5                 if (item is Panel)
 6                 {
 7                     foreach (Control pc in item.Controls)
 8                     {
 9                         if (pc is Label)
10                         {
11                             if (pc.Text == "abc.")
12                                 break;
13                             if (bln)
14                             {
15                                 pc.Text = pc.Text.ToUpper();
16                             }
17                             else
18                             {
19                                 pc.Text = pc.Text.ToLower();
20                             }
21                             break;
22                         }
23                     }
24                 }
25             }
26         }
27 
28         private void CharOrNum()
29         {
30             foreach (Control item in this.tableLayoutPanel2.Controls)
31             {
32                 if (item is Panel)
33                 {
34                     foreach (Control pc in item.Controls)
35                     {
36                         if (pc is Label)
37                         {
38                             string strTag = pc.Text;
39                             pc.Text = pc.Tag.ToString();
40                             pc.Tag = strTag;
41                             break;
42                         }
43                     }
44                 }
45             }
46         }

这样一个键盘就完成了

看下完整代码

  1 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
  2 // 文件名称:UCKeyBorderAll.cs
  3 // 创建日期:2019-08-15 16:00:06
  4 // 功能描述:KeyBord
  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("KeyDown")]
 18     public partial class UCKeyBorderAll : UserControl
 19     {
 20         private KeyBorderCharType _charType = KeyBorderCharType.CHAR;
 21 
 22         [Description("默认显示样式"), Category("自定义")]
 23         public KeyBorderCharType CharType
 24         {
 25             get { return _charType; }
 26             set
 27             {
 28                 _charType = value;
 29                 if (value == KeyBorderCharType.CHAR)
 30                 {
 31                     if (label37.Text.ToLower() == "abc.")
 32                     {
 33                         CharOrNum();
 34                     }
 35                 }
 36                 else
 37                 {
 38                     if (label37.Text.ToLower() == "?123")
 39                     {
 40                         CharOrNum();
 41                     }
 42                 }
 43             }
 44         }
 45         [Description("按键点击事件"), Category("自定义")]
 46         public event EventHandler KeyClick;
 47         [Description("回车点击事件"), Category("自定义")]
 48         public event EventHandler EnterClick;
 49         [Description("删除点击事件"), Category("自定义")]
 50         public event EventHandler BackspaceClike;
 51         [Description("收起点击事件"), Category("自定义")]
 52         public event EventHandler RetractClike;
 53         public UCKeyBorderAll()
 54         {
 55             InitializeComponent();
 56         }
 57 
 58         private void KeyDown_MouseDown(object sender, MouseEventArgs e)
 59         {
 60             Label lbl = sender as Label;
 61             if (string.IsNullOrEmpty(lbl.Text))
 62             {
 63                 return;
 64             }
 65             if (lbl.Text == "大写")
 66             {
 67                 ToUper(true);
 68                 lbl.Text = "小写";
 69             }
 70             else if (lbl.Text == "小写")
 71             {
 72                 ToUper(false);
 73                 lbl.Text = "大写";
 74             }
 75             else if (lbl.Text == "?123" || lbl.Text == "abc.")
 76             {
 77                 CharOrNum();
 78             }
 79             else if (lbl.Text == "空格")
 80             {
 81                 SendKeys.Send(" ");
 82             }
 83             else if (lbl.Text == "删除")
 84             {
 85                 SendKeys.Send("{BACKSPACE}");
 86                 if (BackspaceClike != null)
 87                     BackspaceClike(sender, e);
 88             }
 89             else if (lbl.Text == "回车")
 90             {
 91                 SendKeys.Send("{ENTER}");
 92                 if (EnterClick != null)
 93                     EnterClick(sender, e);
 94             }
 95             else if (lbl.Text == "收起")
 96             {
 97                 if (RetractClike != null)
 98                     RetractClike(sender, e);
 99             }
100             else
101             {
102                 SendKeys.Send(lbl.Text);
103             }
104             if (KeyClick != null)
105                 KeyClick(sender, e);
106         }
107 
108         private void ToUper(bool bln)
109         {
110             foreach (Control item in this.tableLayoutPanel2.Controls)
111             {
112                 if (item is Panel)
113                 {
114                     foreach (Control pc in item.Controls)
115                     {
116                         if (pc is Label)
117                         {
118                             if (pc.Text == "abc.")
119                                 break;
120                             if (bln)
121                             {
122                                 pc.Text = pc.Text.ToUpper();
123                             }
124                             else
125                             {
126                                 pc.Text = pc.Text.ToLower();
127                             }
128                             break;
129                         }
130                     }
131                 }
132             }
133         }
134 
135         private void CharOrNum()
136         {
137             foreach (Control item in this.tableLayoutPanel2.Controls)
138             {
139                 if (item is Panel)
140                 {
141                     foreach (Control pc in item.Controls)
142                     {
143                         if (pc is Label)
144                         {
145                             string strTag = pc.Text;
146                             pc.Text = pc.Tag.ToString();
147                             pc.Tag = strTag;
148                             break;
149                         }
150                     }
151                 }
152             }
153         }
154     }
155     public enum KeyBorderCharType
156     {
157         CHAR = 1,
158         NUMBER = 2
159     }
160 }
View Code

相关文章:

  • 2021-11-18
  • 2022-02-12
  • 2021-09-27
  • 2021-07-14
  • 2021-11-18
  • 2022-02-24
  • 2021-09-14
  • 2021-06-09
猜你喜欢
  • 2022-01-24
  • 2021-11-18
  • 2021-12-16
  • 2021-12-26
  • 2021-12-26
  • 2021-12-07
  • 2021-11-18
相关资源
相似解决方案