Watermark Textbox with Email & number validation




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace Mesoft.Control
{
    public partial class MeTextBox : TextBox
    {
        private string MeTempText = "";
        private Boolean IsNum = false;
        private Boolean IsEmail = false;

        public MeTextBox()
        {
            TextWithWaterMark = false;
            this.Size = new System.Drawing.Size(169, 20);
            this.TextChanged += new System.EventHandler(this.meText_TextChanged);
            this.Leave += new System.EventHandler(this.meText_Leave);
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.meText_KeyPress);
            this.Enter += new System.EventHandler(this.meText_Enter);
            this.Validating += new CancelEventHandler(meText_Validating);
        }
        #region "CUSTOME PROPERTIES"
        [Category("Mesoft")]
        [Description("get or set watermark text")]
        public String WaterMarkText
        {
            get
            {
                return MeTempText;
            }
            set
            {
                MeTempText = value;
                this.Text = MeTempText;
                SetTextColor();
            }
        }
        [Category("Mesoft")]
        public new string Text
        {
            get
            {
                if (!TextWithWaterMark)
                {
                    if (base.Text == WaterMarkText)
                    {
                        return "";
                    }
                    else
                    {
                        return base.Text;
                    }
                }
                else
                {
                    return base.Text;
                }
            }
            set
            {
                base.Text = value;
            }
        }
        [Category("Mesoft")]
        public String TextWithNull
        {
            get
            {
                if (this.Text == WaterMarkText)
                {
                    return "NULL";
                }
                else
                {
                    return Text;
                }
            }
        }
        [Category("Mesoft")]
        [Description("set true metextbox as numbertextbox")]
        public Boolean NumberTextBox
        {
            get
            {
                return IsNum;
            }
            set
            {
                IsNum = value;
                if (IsNum)
                {
                    IsEmail = false;
                }
            }
        }
        [Category("Mesoft")]
        [Description("set true metextbox email validate textbox")]
        public Boolean EmailTextBox
        {
            get
            {
                return IsEmail;
            }
            set
            {
                IsEmail = value;
                if (IsEmail)
                {
                    IsNum = false;
                }
            }
        }
        [Category("Mesoft")]
        [Description("If you want return text with watermark also make it true.")]
        public Boolean TextWithWaterMark
        {
            get;
            set;
        }
        #endregion
        #region "CUSTOME METHOD"
        private void SetTextColor()
        {
            try
            {
                string temp = this.Text;
                if (temp == "")
                {
                    temp = WaterMarkText;
                }
                if (temp == WaterMarkText)
                {
                    this.ForeColor = Color.Silver;
                }
                else
                {
                    this.ForeColor = Color.Black;
                }
            }
            catch (Exception ex)
            {
                General.ErrorMessage(ex.Message);
            }
        }
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
        }
        public new void Clear()
        {
            this.Text = WaterMarkText;
        }
        #endregion
        #region "EVETN"
        private void meText_TextChanged(object sender, EventArgs e)
        {
            try
            {
                SetTextColor();
            }
            catch (Exception ex)
            {
                General.ErrorMessage(ex.Message);
            }
        }
        private void meText_Enter(object sender, EventArgs e)
        {
            try
            {
                string temp = this.Text;
                if (temp == "")
                {
                    temp = WaterMarkText;
                }
                if (temp == WaterMarkText)
                {
                    this.Text = "";
                }
            }
            catch (Exception ex)
            {
                General.ErrorMessage(ex.Message);
            }
        }
        private void meText_Leave(object sender, EventArgs e)
        {
            try
            {
                if (this.Text == "")
                {
                    this.Text = WaterMarkText;
                }
            }
            catch (Exception ex)
            {
                General.ErrorMessage(ex.Message);
            }
        }
        private void meText_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
               if (NumberTextBox)
                {
                    if (!char.IsNumber(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '\b')
                    {
                        e.Handled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                General.ErrorMessage(ex.Message);
            }
        }
        private void meText_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (IsEmail)
                {
                    if (this.Text != "" && this.Text != WaterMarkText)
                    {
                        Mesoft.Extra.Validation ObjVal = new Mesoft.Extra.Validation();
                        ObjVal.IsValidationEmail(this.Text);
                    }
                }
            }
            catch (Exception ex)
            {
                e.Cancel = true;
                General.ErrorMessage(ex.Message);
            }
        }
        #endregion
    }
}

No comments: