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中,英文键盘,数字键盘,支付键盘,手写键盘

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

本篇文章介绍数字键盘和支付键盘,手写键盘将在后面文本框控件介绍是提及到,此处不单独介绍

开始

首先来说数字键盘

添加用户控件,命名UCKeyBorderNum

全部功能代码如下,没有太多东西

 1  private bool useCustomEvent = false;
 2         /// <summary>
 3         /// 是否使用自定义的事件来接收按键,当为true时将不再向系统发送按键请求
 4         /// </summary>
 5         [Description("是否使用自定义的事件来接收按键,当为true时将不再向系统发送按键请求"), Category("自定义")]
 6         public bool UseCustomEvent
 7         {
 8             get { return useCustomEvent; }
 9             set { useCustomEvent = value; }
10         }
11         [Description("数字点击事件"), Category("自定义")]
12         public event EventHandler NumClick;
13         [Description("删除点击事件"), Category("自定义")]
14         public event EventHandler BackspaceClick;
15         [Description("回车点击事件"), Category("自定义")]
16         public event EventHandler EnterClick;
17         public UCKeyBorderNum()
18         {
19             InitializeComponent();
20         }
21 
22         private void Num_MouseDown(object sender, MouseEventArgs e)
23         {
24             if (NumClick != null)
25             {
26                 NumClick(sender, e);
27             }
28             if (useCustomEvent)
29                 return;
30             Label lbl = sender as Label;
31             SendKeys.Send(lbl.Tag.ToString());
32         }
33 
34         private void Backspace_MouseDown(object sender, MouseEventArgs e)
35         {
36             if (BackspaceClick != null)
37             {
38                 BackspaceClick(sender, e);
39             }
40             if (useCustomEvent)
41                 return;
42             Label lbl = sender as Label;
43             SendKeys.Send("{BACKSPACE}");
44         }
45 
46         private void Enter_MouseDown(object sender, MouseEventArgs e)
47         {
48             if (EnterClick != null)
49             {
50                 EnterClick(sender, e);
51             }
52             if (useCustomEvent)
53                 return;
54             SendKeys.Send("{ENTER}");
55         }

看下完整代码

 1 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
 2 // 文件名称:UCKeyBorderNum.cs
 3 // 创建日期:2019-08-15 16:00:10
 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     public partial class UCKeyBorderNum : UserControl
18     {
19         private bool useCustomEvent = false;
20         /// <summary>
21         /// 是否使用自定义的事件来接收按键,当为true时将不再向系统发送按键请求
22         /// </summary>
23         [Description("是否使用自定义的事件来接收按键,当为true时将不再向系统发送按键请求"), Category("自定义")]
24         public bool UseCustomEvent
25         {
26             get { return useCustomEvent; }
27             set { useCustomEvent = value; }
28         }
29         [Description("数字点击事件"), Category("自定义")]
30         public event EventHandler NumClick;
31         [Description("删除点击事件"), Category("自定义")]
32         public event EventHandler BackspaceClick;
33         [Description("回车点击事件"), Category("自定义")]
34         public event EventHandler EnterClick;
35         public UCKeyBorderNum()
36         {
37             InitializeComponent();
38         }
39 
40         private void Num_MouseDown(object sender, MouseEventArgs e)
41         {
42             if (NumClick != null)
43             {
44                 NumClick(sender, e);
45             }
46             if (useCustomEvent)
47                 return;
48             Label lbl = sender as Label;
49             SendKeys.Send(lbl.Tag.ToString());
50         }
51 
52         private void Backspace_MouseDown(object sender, MouseEventArgs e)
53         {
54             if (BackspaceClick != null)
55             {
56                 BackspaceClick(sender, e);
57             }
58             if (useCustomEvent)
59                 return;
60             Label lbl = sender as Label;
61             SendKeys.Send("{BACKSPACE}");
62         }
63 
64         private void Enter_MouseDown(object sender, MouseEventArgs e)
65         {
66             if (EnterClick != null)
67             {
68                 EnterClick(sender, e);
69             }
70             if (useCustomEvent)
71                 return;
72             SendKeys.Send("{ENTER}");
73         }
74     }
75 }
View Code

相关文章:

  • 2022-01-06
  • 2022-02-19
  • 2021-12-07
  • 2021-12-16
  • 2021-12-26
  • 2021-06-15
  • 2021-11-15
  • 2021-12-09
猜你喜欢
  • 2021-10-04
  • 2021-11-18
  • 2021-11-18
  • 2021-11-18
  • 2021-11-18
  • 2021-12-15
  • 2021-11-18
相关资源
相似解决方案