UCKeyBorderAll.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCKeyBorderAll.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCKeyBorderAll.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. [DefaultEvent("KeyDown")]
  32. public partial class UCKeyBorderAll : UserControl
  33. {
  34. /// <summary>
  35. /// The character type
  36. /// </summary>
  37. private KeyBorderCharType _charType = KeyBorderCharType.CHAR;
  38. /// <summary>
  39. /// Gets or sets the type of the character.
  40. /// </summary>
  41. /// <value>The type of the character.</value>
  42. [Description("默认显示样式"), Category("自定义")]
  43. public KeyBorderCharType CharType
  44. {
  45. get { return _charType; }
  46. set
  47. {
  48. _charType = value;
  49. if (value == KeyBorderCharType.CHAR)
  50. {
  51. if (label37.Text.ToLower() == "abc.")
  52. {
  53. CharOrNum();
  54. }
  55. }
  56. else
  57. {
  58. if (label37.Text.ToLower() == "?123")
  59. {
  60. CharOrNum();
  61. }
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// Occurs when [key click].
  67. /// </summary>
  68. [Description("按键点击事件"), Category("自定义")]
  69. public event EventHandler KeyClick;
  70. /// <summary>
  71. /// Occurs when [enter click].
  72. /// </summary>
  73. [Description("回车点击事件"), Category("自定义")]
  74. public event EventHandler EnterClick;
  75. /// <summary>
  76. /// Occurs when [backspace clike].
  77. /// </summary>
  78. [Description("删除点击事件"), Category("自定义")]
  79. public event EventHandler BackspaceClike;
  80. /// <summary>
  81. /// Occurs when [retract clike].
  82. /// </summary>
  83. [Description("收起点击事件"), Category("自定义")]
  84. public event EventHandler RetractClike;
  85. /// <summary>
  86. /// Initializes a new instance of the <see cref="UCKeyBorderAll" /> class.
  87. /// </summary>
  88. public UCKeyBorderAll()
  89. {
  90. InitializeComponent();
  91. }
  92. /// <summary>
  93. /// Handles the MouseDown event of the KeyDown control.
  94. /// </summary>
  95. /// <param name="sender">The source of the event.</param>
  96. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  97. private void KeyDown_MouseDown(object sender, MouseEventArgs e)
  98. {
  99. Label lbl = sender as Label;
  100. if (string.IsNullOrEmpty(lbl.Text))
  101. {
  102. return;
  103. }
  104. if (lbl.Text == "大写")
  105. {
  106. ToUper(true);
  107. lbl.Text = "小写";
  108. }
  109. else if (lbl.Text == "小写")
  110. {
  111. ToUper(false);
  112. lbl.Text = "大写";
  113. }
  114. else if (lbl.Text == "?123" || lbl.Text == "abc.")
  115. {
  116. CharOrNum();
  117. }
  118. else if (lbl.Text == "空格")
  119. {
  120. SendKeys.Send(" ");
  121. }
  122. else if (lbl.Text == "删除")
  123. {
  124. SendKeys.Send("{BACKSPACE}");
  125. if (BackspaceClike != null)
  126. BackspaceClike(sender, e);
  127. }
  128. else if (lbl.Text == "回车")
  129. {
  130. SendKeys.Send("{ENTER}");
  131. if (EnterClick != null)
  132. EnterClick(sender, e);
  133. }
  134. else if (lbl.Text == "收起")
  135. {
  136. if (RetractClike != null)
  137. RetractClike(sender, e);
  138. }
  139. else
  140. {
  141. string Str = "{"+ lbl.Text + "}";
  142. SendKeys.Send(lbl.Text);
  143. }
  144. if (KeyClick != null)
  145. KeyClick(sender, e);
  146. }
  147. /// <summary>
  148. /// Converts to uper.
  149. /// </summary>
  150. /// <param name="bln">if set to <c>true</c> [BLN].</param>
  151. private void ToUper(bool bln)
  152. {
  153. foreach (Control item in this.tableLayoutPanel2.Controls)
  154. {
  155. if (item is Panel)
  156. {
  157. foreach (Control pc in item.Controls)
  158. {
  159. if (pc is Label)
  160. {
  161. if (pc.Text == "abc.")
  162. break;
  163. if (bln)
  164. {
  165. pc.Text = pc.Text.ToUpper();
  166. }
  167. else
  168. {
  169. pc.Text = pc.Text.ToLower();
  170. }
  171. break;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. /// <summary>
  178. /// Characters the or number.
  179. /// </summary>
  180. private void CharOrNum()
  181. {
  182. foreach (Control item in this.tableLayoutPanel2.Controls)
  183. {
  184. if (item is Panel)
  185. {
  186. foreach (Control pc in item.Controls)
  187. {
  188. if (pc is Label)
  189. {
  190. string strTag = pc.Text;
  191. pc.Text = pc.Tag.ToString();
  192. pc.Tag = strTag;
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// Enum KeyBorderCharType
  202. /// </summary>
  203. public enum KeyBorderCharType
  204. {
  205. /// <summary>
  206. /// The character
  207. /// </summary>
  208. CHAR = 1,
  209. /// <summary>
  210. /// The number
  211. /// </summary>
  212. NUMBER = 2
  213. }
  214. }