Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. using System.ComponentModel;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace Promig.Components {
  6.  
  7. public partial class WatermarkMaskedTextBox : MaskedTextBox {
  8.  
  9. //Atributos customizados
  10. private string _watermarkText = "Type here";
  11. public string WatermarkText {
  12. get { return _watermarkText; }
  13. set { _watermarkText = value; }
  14. }
  15.  
  16. private bool _watermarkActive = true;
  17. public bool WatermarkActive {
  18. get { return _watermarkActive; }
  19. set { _watermarkActive = value; }
  20. }
  21.  
  22. private string _maskText = "00000-000";
  23. public string MaskText {
  24. get { return _maskText; }
  25. set { _maskText = value; }
  26. }
  27.  
  28. //Construtores
  29. public WatermarkMaskedTextBox() {
  30.  
  31. this._watermarkActive = true;
  32. this.Text = _watermarkText;
  33. this.ForeColor = Color.Gray;
  34.  
  35. GotFocus += (source, e) => {
  36. RemoveWatermak();
  37.  
  38. };
  39.  
  40. LostFocus += (source, e) => {
  41. ApplyWatermark();
  42. };
  43.  
  44. //Inicializando componente
  45. InitializeComponent();
  46.  
  47. }
  48.  
  49. public WatermarkMaskedTextBox(IContainer container) {
  50.  
  51. this._watermarkActive = true;
  52. this.Text = _watermarkText;
  53. this.ForeColor = Color.Gray;
  54. GotFocus += (source, e) => {
  55. RemoveWatermak();
  56. };
  57.  
  58. LostFocus += (source, e) => {
  59. ApplyWatermark();
  60. };
  61.  
  62. //Inicializando componente
  63. container.Add(this);
  64. InitializeComponent();
  65.  
  66. }
  67.  
  68. //Métodos de Componente
  69. public void RemoveWatermak() {
  70. if (this._watermarkActive) {
  71. this._watermarkActive = false;
  72. this.Text = "";
  73. this.Mask = _maskText;
  74. this.ForeColor = Color.Black;
  75. }
  76. }
  77.  
  78. public void ApplyWatermark() {
  79. if (!this._watermarkActive && Mask.Equals(_maskText) && string.IsNullOrEmpty(this.Text) || ForeColor == Color.Gray) {
  80. this._watermarkActive = true;
  81. this.Mask = "aaaaaaaaaa";
  82. this.Text = _watermarkText;
  83. this.ForeColor = Color.Gray;
  84. }
  85. }
  86. public void ApplyWatermark(string newText) {
  87. WatermarkText = newText;
  88. ApplyWatermark();
  89. }
  90.  
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement