Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Promig.Components {
- public partial class WatermarkMaskedTextBox : MaskedTextBox {
- //Atributos customizados
- private string _watermarkText = "Type here";
- public string WatermarkText {
- get { return _watermarkText; }
- set { _watermarkText = value; }
- }
- private bool _watermarkActive = true;
- public bool WatermarkActive {
- get { return _watermarkActive; }
- set { _watermarkActive = value; }
- }
- private string _maskText = "00000-000";
- public string MaskText {
- get { return _maskText; }
- set { _maskText = value; }
- }
- //Construtores
- public WatermarkMaskedTextBox() {
- this._watermarkActive = true;
- this.Text = _watermarkText;
- this.ForeColor = Color.Gray;
- GotFocus += (source, e) => {
- RemoveWatermak();
- };
- LostFocus += (source, e) => {
- ApplyWatermark();
- };
- //Inicializando componente
- InitializeComponent();
- }
- public WatermarkMaskedTextBox(IContainer container) {
- this._watermarkActive = true;
- this.Text = _watermarkText;
- this.ForeColor = Color.Gray;
- GotFocus += (source, e) => {
- RemoveWatermak();
- };
- LostFocus += (source, e) => {
- ApplyWatermark();
- };
- //Inicializando componente
- container.Add(this);
- InitializeComponent();
- }
- //Métodos de Componente
- public void RemoveWatermak() {
- if (this._watermarkActive) {
- this._watermarkActive = false;
- this.Text = "";
- this.Mask = _maskText;
- this.ForeColor = Color.Black;
- }
- }
- public void ApplyWatermark() {
- if (!this._watermarkActive && Mask.Equals(_maskText) && string.IsNullOrEmpty(this.Text) || ForeColor == Color.Gray) {
- this._watermarkActive = true;
- this.Mask = "aaaaaaaaaa";
- this.Text = _watermarkText;
- this.ForeColor = Color.Gray;
- }
- }
- public void ApplyWatermark(string newText) {
- WatermarkText = newText;
- ApplyWatermark();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement