您現在的位置是:首頁 > 棋牌

(十五)C#WinFrom自定義控制元件系列-鍵盤(二)

  • 由 HZHControls 發表于 棋牌
  • 2021-12-30
簡介Text = values[i]

計算型文字框控制元件怎麼設定

前提

入行已經7,8年了,一直想做一套漂亮點的自定義控制元件,於是就有了本系列文章。

本系列文章將講解各種控制元件的開發及思路,歡迎各位批評指正。

此係列控制元件開發教程將全部在原生控制元件基礎上進行重繪開發,目標的扁平化、漂亮、支援觸屏。

如果有什麼好的建議也可以評論留言來交流。

原始碼地址:

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

碼雲:https://gitee。com/kwwwvagaa/net_winform_custom_control。git

如果覺得寫的還行,請點個 star 支援一下吧

歡迎前來交流探討: 企鵝群568015492

目錄

http://toutiao。com/item/6824291838963220999/

準備工作

鍵盤控制元件目前分為4中,英文鍵盤,數字鍵盤,支付鍵盤,手寫鍵盤

鍵盤一般用在到文字框彈出的鍵盤,那麼為什麼到現在還沒有看到文字框的影子呢?因為文字框的某些功能牽扯到了自定義窗體,所以準備在自定義窗體介紹之後再來說文字框。

本篇文章介紹數字鍵盤和支付鍵盤,手寫鍵盤將在後面文字框控制元件介紹是提及到,此處不單獨介紹

開始

首先來說數字鍵盤

新增使用者控制元件,命名UCKeyBorderNum

全部功能程式碼如下,沒有太多東西

1 private bool useCustomEvent = false; 2 ///

3 /// 是否使用自定義的事件來接收按鍵,當為true時將不再向系統傳送按鍵請求 4 /// 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 }

計效果

(十五)C#WinFrom自定義控制元件系列-鍵盤(二)

下面說支付鍵盤,這個可能就比較小眾的鍵盤了,支援根據輸入金額自動計算可能付款金額

新增使用者控制元件,命名UCKeyBorderPay

同樣的東西不多,主要的就一個計算預估付款金額

1 [Description(“數字點選事件”), Category(“自定義”)] 2 public event EventHandler NumClick; 3 4 [Description(“取消點選事件”), Category(“自定義”)] 5 public event EventHandler CancelClick; 6 7 [Description(“確定點選事件”), Category(“自定義”)] 8 public event EventHandler OKClick; 9 10 [Description(“刪除點選事件”), Category(“自定義”)] 11 public event EventHandler BackspaceClick; 12 13 [Description(“金額點選事件”), Category(“自定義”)] 14 public event EventHandler MoneyClick; 15 public UCKeyBorderPay() 16 { 17 InitializeComponent(); 18 } 19 20 #region 設定快速付款金額 21 ///

22 /// 功能描述:設定快速付款金額 23 /// 作者:HZH 24 /// 建立日期:2019-03-07 11:41:04 25 /// 任務編號:POS 26 /// 27 /// SorceMoney 28 public void SetPayMoney(decimal SorceMoney) 29 { 30 List list = new List(); 31 decimal d = Math。Ceiling(SorceMoney); 32 if (SorceMoney > 0m) 33 { 34 if (SorceMoney < 5m) 35 { 36 list。Add(5m); 37 list。Add(10m); 38 list。Add(20m); 39 list。Add(50m); 40 } 41 else if (SorceMoney < 10m) 42 { 43 list。Add(10m); 44 list。Add(20m); 45 list。Add(50m); 46 list。Add(100m); 47 } 48 else 49 { 50 int num = Convert。ToInt32(d % 10m); 51 int num2 = Convert。ToInt32(Math。Floor(d / 10m) % 10m); 52 int num3 = Convert。ToInt32(Math。Floor(d / 100m)); 53 int num4; 54 if (num < 5) 55 { 56 num4 = num2 * 10 + 5; 57 list。Add(num4 + num3 * 100); 58 num4 = (num2 + 1) * 10; 59 list。Add(num4 + num3 * 100); 60 } 61 else 62 { 63 num4 = (num2 + 1) * 10; 64 list。Add(num4 + num3 * 100); 65 } 66 if (num4 >= 0 && num4 < 10) 67 { 68 num4 = 10; 69 if (list。Count < 4) 70 { 71 list。Add(num4 + num3 * 100); 72 } 73 num4 = 20; 74 if (list。Count < 4) 75 { 76 list。Add(num4 + num3 * 100); 77 } 78 num4 = 50; 79 if (list。Count < 4) 80 { 81 list。Add(num4 + num3 * 100); 82 } 83 num4 = 100; 84 if (list。Count < 4) 85 { 86 list。Add(num4 + num3 * 100); 87 } 88 } 89 else if (num4 >= 10 && num4 < 20) 90 { 91 num4 = 20; 92 if (list。Count < 4) 93 { 94 list。Add(num4 + num3 * 100); 95 } 96 num4 = 50; 97 if (list。Count < 4) 98 { 99 list。Add(num4 + num3 * 100);100 }101 num4 = 100;102 if (list。Count < 4)103 {104 list。Add(num4 + num3 * 100);105 }106 }107 else if (num4 >= 20 && num4 < 50)108 {109 num4 = 50;110 if (list。Count < 4)111 {112 list。Add(num4 + num3 * 100);113 }114 num4 = 100;115 if (list。Count < 4)116 {117 list。Add(num4 + num3 * 100);118 }119 }120 else if (num4 < 100)121 {122 num4 = 100;123 if (list。Count < 4)124 {125 list。Add(num4 + num3 * 100);126 }127 }128 }129 }130 SetFastMoneyToContrl(list);131 }132 #endregion133 134 private void SetFastMoneyToContrl(List values)135 {136 List
Top