IKMetroTextBox.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using MetroFramework.Controls;
  2. using System;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. namespace Infoking.Common.Windows.Control
  6. {
  7. public class IKMetroTextBox : MetroTextBox
  8. {
  9. private Container components;
  10. private bool _HaveChanged;
  11. [ReadOnly(true)]
  12. [Category("BizPeri Configuration")]
  13. [DefaultValue(false)]
  14. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  15. [Description("Get or Set whether the control changed.")]
  16. public bool HaveChanged
  17. {
  18. get
  19. {
  20. return _HaveChanged;
  21. }
  22. set
  23. {
  24. _HaveChanged = value;
  25. }
  26. }
  27. [Category("BizPeri Configuration")]
  28. [ReadOnly(true)]
  29. [Description("Get or Set text.")]
  30. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  31. public string CValue
  32. {
  33. get
  34. {
  35. return Text;
  36. }
  37. set
  38. {
  39. Text = value;
  40. }
  41. }
  42. public IntPtr TextBoxHandle
  43. {
  44. get
  45. {
  46. try
  47. {
  48. return base.Handle;
  49. }
  50. catch
  51. {
  52. return IntPtr.Zero;
  53. }
  54. }
  55. }
  56. public event KeyPressEventHandler OnEnterPress;
  57. #region ReadOnly Change
  58. private static readonly object EVENT_READONLYCHANGED = new object();
  59. [DefaultValue(false)]
  60. [Description("TextBoxReadOnlyDescr")]
  61. [RefreshProperties(RefreshProperties.Repaint)]
  62. [Category("CatBehavior")]
  63. public new bool ReadOnly
  64. {
  65. get
  66. {
  67. return base.ReadOnly;
  68. }
  69. set
  70. {
  71. base.ReadOnly = value;
  72. OnReadOnlyChanged(EventArgs.Empty);
  73. }
  74. }
  75. [Description("TextBoxBaseOnReadOnlyChangedDescr")]
  76. [Category("CatPropertyChanged")]
  77. public event EventHandler ReadOnlyChanged
  78. {
  79. add
  80. {
  81. base.Events.AddHandler(EVENT_READONLYCHANGED, value);
  82. }
  83. remove
  84. {
  85. base.Events.RemoveHandler(EVENT_READONLYCHANGED, value);
  86. }
  87. }
  88. protected virtual void OnReadOnlyChanged(EventArgs e)
  89. {
  90. (base.Events[EVENT_READONLYCHANGED] as EventHandler)?.Invoke(this, e);
  91. }
  92. #endregion
  93. public IKMetroTextBox()
  94. {
  95. InitializeComponent();
  96. base.TextChanged += IKTextBox_TextChanged;
  97. this.ReadOnlyChanged += IKTextBox_ReadOnlyChanged;
  98. }
  99. private void InitializeComponent()
  100. {
  101. components = new System.ComponentModel.Container();
  102. }
  103. protected override void OnPaint(PaintEventArgs pe)
  104. {
  105. base.OnPaint(pe);
  106. }
  107. protected override void OnGotFocus(EventArgs e)
  108. {
  109. SelectAll();
  110. }
  111. protected override void OnLostFocus(EventArgs e)
  112. {
  113. Select(0, 0);
  114. }
  115. protected override void OnKeyPress(KeyPressEventArgs e)
  116. {
  117. if (e.KeyChar == '\r')
  118. {
  119. if (this.OnEnterPress != null)
  120. {
  121. this.OnEnterPress(this, e);
  122. }
  123. e.Handled = true;
  124. }
  125. else
  126. {
  127. base.OnKeyPress(e);
  128. }
  129. }
  130. private void IKTextBox_TextChanged(object sender, EventArgs e)
  131. {
  132. _HaveChanged = true;
  133. }
  134. private void IKTextBox_ReadOnlyChanged(object sender, EventArgs e)
  135. {
  136. base.TabStop = !base.ReadOnly;
  137. }
  138. public void EnterPress(KeyPressEventArgs e)
  139. {
  140. if (this.OnEnterPress != null)
  141. {
  142. this.OnEnterPress(this, e);
  143. }
  144. }
  145. }
  146. }