TextBoxEx.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace OpenSEMI.Ctrlib.Controls
  17. {
  18. public enum EditBoxMode
  19. {
  20. Default,
  21. SignInteger,
  22. UnSignInteger,
  23. Decimal,
  24. UnSignDecimal,
  25. Email,
  26. Time,
  27. FileName
  28. }
  29. public class TextBoxEx : TextBox
  30. {
  31. public event KeyEventHandler TextBoxExEnterKeyDown;
  32. #region define fields & properties
  33. private string m_strOldValue = string.Empty;
  34. public EditBoxMode EditBoxMode
  35. {
  36. get { return (EditBoxMode)GetValue(EditBoxModeProperty); }
  37. set { SetValue(EditBoxModeProperty, value); }
  38. }
  39. public static readonly DependencyProperty EditBoxModeProperty = DependencyProperty.Register("EditBoxMode", typeof(EditBoxMode), typeof(TextBoxEx), new UIPropertyMetadata(EditBoxMode.Default));
  40. #region NormalColor (DependencyProperty)
  41. public static readonly DependencyProperty NormalColorProperty = DependencyProperty.Register("NormalColor", typeof(Brush), typeof(TextBoxEx));
  42. public Brush NormalColor
  43. {
  44. get { return (Brush)GetValue(NormalColorProperty); }
  45. set { SetValue(NormalColorProperty, value); }
  46. }
  47. #endregion
  48. #region ChangedColor (DependencyProperty)
  49. public static readonly DependencyProperty ChangedColorProperty = DependencyProperty.Register("ChangedColor", typeof(Brush), typeof(TextBoxEx));
  50. public Brush ChangedColor
  51. {
  52. get { return (Brush)GetValue(ChangedColorProperty); }
  53. set { SetValue(ChangedColorProperty, value); }
  54. }
  55. #endregion
  56. #region WarningColor (DependencyProperty)
  57. public static readonly DependencyProperty WarningColorProperty = DependencyProperty.Register("WarningColor", typeof(Brush), typeof(TextBoxEx));
  58. public Brush WarningColor
  59. {
  60. get { return (Brush)GetValue(WarningColorProperty); }
  61. set { SetValue(WarningColorProperty, value); }
  62. }
  63. #endregion
  64. private bool m_AllowEmpty = true;
  65. public bool AllowEmpty
  66. {
  67. get { return m_AllowEmpty; }
  68. set { m_AllowEmpty = value; }
  69. }
  70. /// <summary>
  71. /// This is a flag to indicate that whether to change the bg color
  72. /// when the text changed by backgroud not by GUI side.
  73. /// This flag is true when control is bound to display text from dialog
  74. /// </summary>
  75. private bool m_AllowBackgroundChange = true;
  76. public bool AllowBackgroundChange
  77. {
  78. get { return m_AllowBackgroundChange; }
  79. set { m_AllowBackgroundChange = value; }
  80. }
  81. private bool m_AllowIsOutOfRangeSaveChange = true;
  82. public bool AllowIsOutOfRangeSaveChange
  83. {
  84. get { return m_AllowIsOutOfRangeSaveChange; }
  85. set { m_AllowIsOutOfRangeSaveChange = value; }
  86. }
  87. private int m_KeepDecimals = -1;
  88. public int KeepDecimals
  89. {
  90. get { return m_KeepDecimals; }
  91. set { m_KeepDecimals = value; }
  92. }
  93. private int m_KeepIntegers = -1;
  94. public int KeepIntegers
  95. {
  96. get { return m_KeepIntegers; }
  97. set { m_KeepIntegers = value; }
  98. }
  99. public override void OnApplyTemplate()
  100. {
  101. base.OnApplyTemplate();
  102. //this.TextSaved = true;
  103. }
  104. #region MaxValue (DependencyProperty)
  105. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register("MaxValue", typeof(double), typeof(TextBoxEx), new FrameworkPropertyMetadata(double.NaN) { BindsTwoWayByDefault = true });
  106. public double MaxValue
  107. {
  108. get { return (double)GetValue(MaxValueProperty); }
  109. set { SetValue(MaxValueProperty, value); }
  110. }
  111. #endregion
  112. #region MinValue (DependencyProperty)
  113. public double MinValue
  114. {
  115. get { return (double)GetValue(MinValueProperty); }
  116. set { SetValue(MinValueProperty, value); }
  117. }
  118. public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register("MinValue", typeof(double), typeof(TextBoxEx), new UIPropertyMetadata(double.NaN));
  119. #endregion
  120. #region Accuracy (DependencyProperty)
  121. public Int32 Accuracy
  122. {
  123. get { return (Int32)GetValue(AccuracyProperty); }
  124. set { SetValue(AccuracyProperty, value); }
  125. }
  126. public static readonly DependencyProperty AccuracyProperty = DependencyProperty.Register("Accuracy", typeof(Int32), typeof(TextBoxEx), new UIPropertyMetadata(4));
  127. #endregion
  128. #region TextSaved (DependencyProperty)
  129. public bool TextSaved
  130. {
  131. get { return (bool)GetValue(TextSavedProperty); }
  132. set { SetValue(TextSavedProperty, value); }
  133. }
  134. public static readonly DependencyProperty TextSavedProperty =
  135. DependencyProperty.Register("TextSaved", typeof(bool), typeof(TextBoxEx),
  136. new UIPropertyMetadata(true, new PropertyChangedCallback(TextSavedChangedCallBack)));
  137. #endregion
  138. #region IsScrollToEnd (DependencyProperty)
  139. public Boolean IsScrollToEnd
  140. {
  141. get { return (Boolean)GetValue(ScrollToEndProperty); }
  142. set { SetValue(ScrollToEndProperty, value); }
  143. }
  144. public static readonly DependencyProperty ScrollToEndProperty = DependencyProperty.Register("IsScrollToEnd", typeof(Boolean), typeof(TextBoxEx), new UIPropertyMetadata(false));
  145. #endregion
  146. public Boolean IsTextboxFocused
  147. {
  148. get { return (Boolean)GetValue(IsTextboxFocusedProperty); }
  149. set { SetValue(IsTextboxFocusedProperty, value); }
  150. }
  151. public static readonly DependencyProperty IsTextboxFocusedProperty = DependencyProperty.Register("IsTextboxFocused", typeof(Boolean), typeof(TextBoxEx), new UIPropertyMetadata(false));
  152. #endregion
  153. static TextBoxEx()
  154. {
  155. DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxEx), new FrameworkPropertyMetadata(typeof(TextBoxEx)));
  156. }
  157. #region Input Control
  158. protected override void OnPreviewKeyDown(KeyEventArgs e)
  159. {
  160. switch (this.EditBoxMode)
  161. {
  162. case EditBoxMode.SignInteger:
  163. AllowInteger(e);
  164. break;
  165. case EditBoxMode.UnSignInteger:
  166. AllowUnsignInteger(e);
  167. break;
  168. case EditBoxMode.Decimal:
  169. AllowDecimal(e);
  170. break;
  171. case EditBoxMode.UnSignDecimal:
  172. AllowUnSignDecimal(e);
  173. break;
  174. case EditBoxMode.Time:
  175. AllowTime(e);
  176. break;
  177. case EditBoxMode.FileName:
  178. AllowFileName(e);
  179. break;
  180. }
  181. }
  182. private void AllowInteger(KeyEventArgs e)
  183. {
  184. bool isControl = ((Keyboard.Modifiers != ModifierKeys.None && Keyboard.Modifiers != ModifierKeys.Shift)
  185. || e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Insert
  186. || e.Key == Key.Down || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up
  187. || e.Key == Key.Tab
  188. || e.Key == Key.PageDown || e.Key == Key.PageUp
  189. || e.Key == Key.Enter || e.Key == Key.Return || e.Key == Key.Escape
  190. || e.Key == Key.Home || e.Key == Key.End);
  191. //if (this.Text.IndexOfAny(new char[] { '-' }, 0) > -1 && this.CaretIndex == 0 && !isControl) //Disable input before minus
  192. //{
  193. // e.Handled = true;
  194. // return;
  195. //}
  196. bool isNumPadNumeric = (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9);
  197. bool isNumeric = (e.Key >= Key.D0 && e.Key <= Key.D9);
  198. if ((isNumeric || isNumPadNumeric) && Keyboard.Modifiers != ModifierKeys.None)
  199. {
  200. e.Handled = true;
  201. return;
  202. }
  203. //Minus
  204. if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
  205. {
  206. if (Keyboard.Modifiers == ModifierKeys.Shift)
  207. {
  208. isControl = false;
  209. }
  210. else
  211. {
  212. if (this.Text.IndexOfAny(new char[] { '-' }, 0) == -1 && this.CaretIndex == 0)
  213. {
  214. isControl = true;
  215. }
  216. else
  217. {
  218. e.Handled = true;
  219. return;
  220. }
  221. }
  222. }
  223. e.Handled = !isControl && !isNumeric && !isNumPadNumeric;
  224. }
  225. private void AllowUnsignInteger(KeyEventArgs e)
  226. {
  227. bool isNumPadNumeric = (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9);
  228. bool isNumeric = (e.Key >= Key.D0 && e.Key <= Key.D9);
  229. if ((isNumeric || isNumPadNumeric) && Keyboard.Modifiers != ModifierKeys.None)
  230. {
  231. e.Handled = true;
  232. return;
  233. }
  234. bool isControl = ((Keyboard.Modifiers != ModifierKeys.None && Keyboard.Modifiers != ModifierKeys.Shift)
  235. || e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Insert
  236. || e.Key == Key.Down || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up
  237. || e.Key == Key.Tab
  238. || e.Key == Key.PageDown || e.Key == Key.PageUp
  239. || e.Key == Key.Enter || e.Key == Key.Return || e.Key == Key.Escape
  240. || e.Key == Key.Home || e.Key == Key.End);
  241. e.Handled = !isControl && !isNumeric && !isNumPadNumeric;
  242. }
  243. private void AllowDecimal(KeyEventArgs e)
  244. {
  245. bool isControl = ((Keyboard.Modifiers != ModifierKeys.None && Keyboard.Modifiers != ModifierKeys.Shift)
  246. || e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Insert
  247. || e.Key == Key.Down || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up
  248. || e.Key == Key.Tab
  249. || e.Key == Key.PageDown || e.Key == Key.PageUp
  250. || e.Key == Key.Enter || e.Key == Key.Return || e.Key == Key.Escape
  251. || e.Key == Key.Home || e.Key == Key.End);
  252. //if (this.Text.IndexOfAny(new char[] { '-' }, 0) > -1 && this.CaretIndex == 0 && !isControl) //Disable input before minus
  253. //{
  254. // e.Handled = true;
  255. // return;
  256. //}
  257. bool isNumPadNumeric = (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9);
  258. bool isNumeric = (e.Key >= Key.D0 && e.Key <= Key.D9);
  259. if ((isNumeric || isNumPadNumeric) && Keyboard.Modifiers != ModifierKeys.None)
  260. {
  261. e.Handled = true;
  262. return;
  263. }
  264. //Minus
  265. if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
  266. {
  267. if (Keyboard.Modifiers == ModifierKeys.Shift)
  268. {
  269. isControl = false;
  270. }
  271. else
  272. {
  273. if (this.Text.IndexOfAny(new char[] { '-' }, 0) == -1 && this.CaretIndex == 0)
  274. {
  275. isControl = true;
  276. }
  277. else
  278. {
  279. e.Handled = true;
  280. return;
  281. }
  282. }
  283. }
  284. //Decimal point
  285. if (e.Key == Key.OemPeriod || e.Key == Key.Decimal)
  286. {
  287. if (Keyboard.Modifiers == ModifierKeys.Shift)
  288. {
  289. isControl = false;
  290. }
  291. else
  292. {
  293. if (this.Text.IndexOfAny(new char[] { '.' }, 0) == -1 && this.CaretIndex > 0
  294. && this.CaretIndex + this.Accuracy >= this.Text.Length) //Accuracy
  295. {
  296. isControl = true;
  297. }
  298. else
  299. {
  300. e.Handled = true;
  301. return;
  302. }
  303. }
  304. }
  305. //Accuracy
  306. int pointIndex = this.Text.IndexOf('.');
  307. if (pointIndex > -1 && this.CaretIndex > pointIndex)
  308. {
  309. if (this.Text.Length - pointIndex > this.Accuracy)
  310. {
  311. isNumeric = isNumPadNumeric = false;
  312. }
  313. }
  314. e.Handled = !isControl && !isNumeric && !isNumPadNumeric;
  315. }
  316. private void AllowUnSignDecimal(KeyEventArgs e)
  317. {
  318. bool isNumPadNumeric = (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9);
  319. bool isNumeric = (e.Key >= Key.D0 && e.Key <= Key.D9);
  320. if ((isNumeric || isNumPadNumeric) && Keyboard.Modifiers != ModifierKeys.None)
  321. {
  322. e.Handled = true;
  323. return;
  324. }
  325. bool isControl = ((Keyboard.Modifiers != ModifierKeys.None && Keyboard.Modifiers != ModifierKeys.Shift)
  326. || e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Insert
  327. || e.Key == Key.Down || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up
  328. || e.Key == Key.Tab
  329. || e.Key == Key.PageDown || e.Key == Key.PageUp
  330. || e.Key == Key.Enter || e.Key == Key.Return || e.Key == Key.Escape
  331. || e.Key == Key.Home || e.Key == Key.End);
  332. //Decimal point
  333. if (e.Key == Key.OemPeriod || e.Key == Key.Decimal)
  334. {
  335. if (Keyboard.Modifiers == ModifierKeys.Shift)
  336. {
  337. isControl = false;
  338. }
  339. else
  340. {
  341. if (this.Text.IndexOfAny(new char[] { '.' }, 0) == -1 && this.CaretIndex > 0
  342. && this.CaretIndex + this.Accuracy >= this.Text.Length) //Accuracy
  343. {
  344. isControl = true;
  345. }
  346. else
  347. {
  348. e.Handled = true;
  349. return;
  350. }
  351. }
  352. }
  353. //Accuracy
  354. int pointIndex = this.Text.IndexOf('.');
  355. if (pointIndex > -1 && this.CaretIndex > pointIndex)
  356. {
  357. if (this.Text.Length - pointIndex > this.Accuracy)
  358. {
  359. isNumeric = isNumPadNumeric = false;
  360. }
  361. }
  362. e.Handled = !isControl && !isNumeric && !isNumPadNumeric;
  363. }
  364. private void AllowTime(KeyEventArgs e)
  365. {
  366. AllowDecimal(e);
  367. if (e.Key == Key.Oem1 && Keyboard.Modifiers == ModifierKeys.Shift)
  368. e.Handled = false;
  369. if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
  370. e.Handled = true;
  371. }
  372. private void AllowFileName(KeyEventArgs e)
  373. {
  374. if (this.Text.Length >= 128)
  375. {
  376. e.Handled = true;
  377. return;
  378. }
  379. if (Keyboard.Modifiers == ModifierKeys.Shift)
  380. {
  381. if (e.Key == Key.OemPeriod || //>
  382. e.Key == Key.OemComma || //<
  383. e.Key == Key.D8 || //*
  384. e.Key == Key.Oem1 || //:
  385. e.Key == Key.Oem7 //"
  386. )
  387. {
  388. e.Handled = true;
  389. return;
  390. }
  391. }
  392. if (e.Key == Key.Oem2 || //? /
  393. e.Key == Key.Oem5) //\ |
  394. {
  395. e.Handled = true;
  396. return;
  397. }
  398. }
  399. #endregion
  400. #region Range Control
  401. public bool IsOutOfRange()
  402. {
  403. if (!double.IsNaN(this.MinValue) && !double.IsNaN(this.MaxValue))
  404. {
  405. double value;
  406. bool m_flag = double.TryParse(this.Text, out value);
  407. if (value > this.MaxValue)
  408. return true;
  409. else if (value < this.MinValue)
  410. return true;
  411. else
  412. return false;
  413. }
  414. return false;
  415. }
  416. public bool IsOutOfRange(double value)
  417. {
  418. if (!double.IsNaN(this.MinValue) && !double.IsNaN(this.MaxValue))
  419. {
  420. if (value > this.MaxValue)
  421. return true;
  422. else if (value < this.MinValue)
  423. return true;
  424. else
  425. return false;
  426. }
  427. return false;
  428. }
  429. protected override void OnLostFocus(RoutedEventArgs e)
  430. {
  431. base.OnLostFocus(e);
  432. this.IsTextboxFocused = false;
  433. if (this.Text.Length == 0)
  434. {
  435. if (!AllowEmpty)
  436. this.Text = m_strOldValue;
  437. return;
  438. }
  439. else
  440. {
  441. if (EditBoxMode == EditBoxMode.UnSignDecimal || EditBoxMode == EditBoxMode.Decimal || EditBoxMode == EditBoxMode.SignInteger || EditBoxMode == EditBoxMode.UnSignInteger)
  442. {
  443. if (IsOutOfRange())
  444. {
  445. this.Background = Brushes.Red;
  446. this.ToolTip = this.MinValue + "-" + this.MaxValue;
  447. }
  448. else
  449. {
  450. SetBGColor(this);
  451. }
  452. }
  453. }
  454. }
  455. #endregion
  456. #region Expose an event to support save function by ENTER key
  457. protected override void OnKeyDown(KeyEventArgs e)
  458. {
  459. if (e.Key == Key.Tab)
  460. {
  461. this.SelectAll();
  462. }
  463. KeyEventHandler handler = this.TextBoxExEnterKeyDown;
  464. if (handler != null && e.Key == Key.Enter)
  465. {
  466. if (IsOutOfRange())
  467. {
  468. this.Background = Brushes.Red; //this.WarningColor
  469. this.ToolTip = this.MinValue + "-" + this.MaxValue;
  470. }
  471. else
  472. handler(this, e);
  473. }
  474. if (e.Key == Key.Return || e.Key == Key.Enter)
  475. {
  476. if (IsOutOfRange())
  477. {
  478. this.Background = Brushes.Red;
  479. this.ToolTip = this.MinValue + "-" + this.MaxValue;
  480. }
  481. else
  482. {
  483. SetBGColor(this);
  484. }
  485. }
  486. }
  487. #endregion
  488. protected override void OnTextChanged(TextChangedEventArgs e)
  489. {
  490. base.OnTextChanged(e);
  491. if (IsInitialized)
  492. {
  493. //text changed because of binding changed
  494. if (m_strOldValue != this.Text && AllowBackgroundChange)// && this.IsFocused
  495. {
  496. //this.TextSaved = false;
  497. }
  498. else
  499. {
  500. this.m_strOldValue = this.Text;
  501. // this.TextSaved = true;
  502. }
  503. if (EditBoxMode == EditBoxMode.Default)
  504. {
  505. this.m_strOldValue = this.Text;
  506. }
  507. else if (EditBoxMode == EditBoxMode.FileName)
  508. {
  509. Regex regex = new Regex("\\*|\\\\|\\/|\\?|\"|:|\\<|\\>|\\|"); //*:"<>?/\|
  510. this.Text = regex.Replace(this.Text, String.Empty);
  511. }
  512. else
  513. {
  514. double.TryParse(this.Text, out double controlValue);
  515. if (IsOutOfRange(controlValue))
  516. {
  517. this.Background = Brushes.Red; //this.WarningColor
  518. this.ToolTip = this.MinValue + "-" + this.MaxValue;
  519. }
  520. else
  521. {
  522. SetBGColor(this);
  523. this.ToolTip = this.MinValue + "-" + this.MaxValue;
  524. }
  525. if (KeepDecimals != -1)
  526. {
  527. if (KeepIntegers != -1)
  528. {
  529. StringBuilder strFormat = new StringBuilder();
  530. for (int i = 0; i < KeepIntegers; i++)
  531. {
  532. strFormat.Append("0");
  533. }
  534. strFormat.Append(".");
  535. for (int i = 0; i < KeepDecimals; i++)
  536. {
  537. strFormat.Append("0");
  538. }
  539. Text = controlValue.ToString(strFormat.ToString());
  540. }
  541. else
  542. { Text = controlValue.ToString($"f{KeepDecimals}"); }
  543. }
  544. else
  545. {
  546. if (KeepIntegers != -1)
  547. {
  548. Text = ((int)controlValue).ToString($"D{KeepIntegers}");
  549. }
  550. else
  551. { Text = controlValue.ToString(); }
  552. }
  553. }
  554. }
  555. if (IsScrollToEnd)
  556. {
  557. this.ScrollToEnd();
  558. }
  559. }
  560. protected override void OnMouseDown(MouseButtonEventArgs e)
  561. {
  562. base.OnMouseDown(e);
  563. if (e.ClickCount == 2)
  564. {
  565. this.SelectAll();
  566. }
  567. }
  568. protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
  569. {
  570. base.OnGotKeyboardFocus(e);
  571. this.IsTextboxFocused = true;
  572. }
  573. private static void TextSavedChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs args)
  574. {
  575. TextBoxEx m_txt = d as TextBoxEx;
  576. if (m_txt != null)
  577. {
  578. if (m_txt.IsOutOfRange())
  579. {
  580. m_txt.Background = Brushes.Red;
  581. m_txt.ToolTip = m_txt.MinValue + "-" + m_txt.MaxValue;
  582. }
  583. else
  584. {
  585. SetBGColor(m_txt);
  586. }
  587. }
  588. }
  589. /// <summary>
  590. /// Set the Background of textbox according to the TextSaved property
  591. /// </summary>
  592. private static void SetBGColor(TextBoxEx tb)
  593. {
  594. if (tb.TextSaved)
  595. {
  596. tb.m_strOldValue = tb.Text;
  597. if (tb.NormalColor != null)
  598. tb.Background = tb.NormalColor;
  599. }
  600. else
  601. {
  602. if (tb.ChangedColor != null)
  603. tb.Background = tb.ChangedColor;
  604. }
  605. }
  606. protected override void OnMouseEnter(MouseEventArgs e)
  607. {
  608. base.OnMouseEnter(e);
  609. this.Focus();
  610. }
  611. protected override void OnMouseLeave(MouseEventArgs e)
  612. {
  613. base.OnMouseLeave(e);
  614. }
  615. public void SaveText()
  616. {
  617. this.TextSaved = true;
  618. }
  619. }
  620. }