CTextBox.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Controls.Properties;
  11. namespace Controls
  12. {
  13. public delegate void DTextStrChanged(string textStr);
  14. public partial class CTextBox : UserControl
  15. {
  16. public CTextBox()
  17. {
  18. InitializeComponent();
  19. tbx_text.Text = DefaultText;
  20. //if (PasswordChar && tbx_text.Text != DefaultText)
  21. //{
  22. //}
  23. //else
  24. //{
  25. //}
  26. //if (PasswordChar)
  27. //{
  28. // tbx_text.PasswordChar = '*';
  29. // btn_eye.Visible = true;
  30. //}
  31. //else
  32. //{
  33. //}
  34. if (!PasswordChar)
  35. {
  36. tbx_text.PasswordChar = '\0';
  37. btn_eye.Visible = false;
  38. }
  39. else
  40. {
  41. tbx_text.PasswordChar = '\0';
  42. btn_eye.Visible = true ;
  43. }
  44. }
  45. /// <summary>
  46. /// 文本改变事件
  47. /// </summary>
  48. public event DTextStrChanged TextStrChanged;
  49. /// <summary>
  50. /// 控件文本
  51. /// </summary>
  52. private string _text = string.Empty;
  53. public string TextStr
  54. {
  55. get
  56. {
  57. return _text;
  58. }
  59. set
  60. {
  61. if (value == string.Empty)
  62. {
  63. tbx_text.PasswordChar = '\0';
  64. tbx_text.ForeColor = Color.DarkGray;
  65. tbx_text.Text = DefaultText;
  66. _text = value;
  67. }
  68. else
  69. {
  70. if (PasswordChar)
  71. tbx_text.PasswordChar = '*';
  72. else
  73. tbx_text.PasswordChar = '\0';
  74. tbx_text.ForeColor = Color.FromArgb(64, 64, 64);
  75. _text = value;
  76. tbx_text.Text = _text;
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// 默认文本
  82. /// </summary>
  83. private string _defaultText = string.Empty;
  84. public string DefaultText
  85. {
  86. get { return _defaultText; }
  87. set { _defaultText = value; }
  88. }
  89. /// <summary>
  90. /// 是否以密码形式显示
  91. /// </summary>
  92. private bool _passwordChar = false;
  93. public bool PasswordChar
  94. {
  95. get { return _passwordChar; }
  96. set
  97. {
  98. _passwordChar = value;
  99. if (PasswordChar)
  100. {
  101. tbx_text.PasswordChar = '*';
  102. }
  103. else
  104. {
  105. tbx_text.PasswordChar = '\0';
  106. }
  107. }
  108. }
  109. private void TextBox_Enter(object sender, EventArgs e)
  110. {
  111. lbl_line.Height = 2;
  112. lbl_line.BackColor = Color.FromArgb(18, 150, 219);
  113. if (tbx_text.Text == DefaultText)
  114. {
  115. tbx_text.SelectionStart = 0;
  116. tbx_text.SelectionLength = 0;
  117. }
  118. }
  119. private void TextBox_Leave(object sender, EventArgs e)
  120. {
  121. lbl_line.Height = 1;
  122. lbl_line.BackColor = Color.Gray;
  123. }
  124. private void tbx_text_TextChanged(object sender, EventArgs e)
  125. {
  126. if (tbx_text.Text != DefaultText && tbx_text.Text != TextStr)
  127. TextStr = tbx_text.Text;
  128. if (TextStrChanged != null)
  129. TextStrChanged(TextStr);
  130. if (PasswordChar && TextStr != string.Empty)
  131. tbx_text.PasswordChar = '*';
  132. else if (TextStr ==string .Empty )
  133. tbx_text.PasswordChar = '\0';
  134. }
  135. private void tbx_text_KeyDown(object sender, KeyEventArgs e)
  136. {
  137. if (tbx_text.Text == DefaultText && e.KeyCode != Keys.Back)
  138. tbx_text.Text = string.Empty;
  139. }
  140. private void tbx_text_MouseUp(object sender, MouseEventArgs e)
  141. {
  142. if (e.Button == MouseButtons.Left)
  143. {
  144. if (tbx_text.Text == DefaultText)
  145. {
  146. tbx_text.SelectionStart = 0;
  147. tbx_text.SelectionLength = 0;
  148. }
  149. }
  150. }
  151. private void btn_eye_MouseDown(object sender, MouseEventArgs e)
  152. {
  153. tbx_text.PasswordChar = '\0';
  154. btn_eye.BackgroundImage = Resources.Show;
  155. //tbx_text.Width = tbx_text.Width - 30;
  156. }
  157. private void btn_eye_MouseUp(object sender, MouseEventArgs e)
  158. {
  159. if (TextStr != string.Empty)
  160. tbx_text.PasswordChar = '*';
  161. btn_eye.BackgroundImage = Resources.Hide;
  162. tbx_text.Width = tbx_text.Width + 30;
  163. tbx_text.Focus();
  164. tbx_text.SelectionStart = 0;
  165. tbx_text.SelectionLength = 0;
  166. }
  167. private void TextBox_Load(object sender, EventArgs e)
  168. {
  169. if (!PasswordChar)
  170. {
  171. tbx_text.PasswordChar = '\0';
  172. btn_eye.Visible = false;
  173. }
  174. else
  175. {
  176. tbx_text.PasswordChar = '\0';
  177. btn_eye.Visible = true ;
  178. }
  179. }
  180. }
  181. }