Sunday, March 2, 2014

Custom Richtextbox control with Watermark Support in C#

There was a solution in MSDN Forums on how to add watermark to a Textbox control. I tried replacing the base class if this will work on a RichTextbox control. After testing, the custom control works as expected.
Code:
/// <summary>
    /// Set Watermark in RichTextbox.
    /// </summary>
    public class WatermarkRichTextbox : RichTextBox
    {
        private string watermark;
        private Color watermarkColor;
        private Color foreColor;
        private bool empty;

        [Browsable (true)]
        public Color WatermarkColor 
        {
            get 
            { 
                return watermarkColor; 
            }
            set 
            {
                watermarkColor = value;
                if (empty) 
                {
                    base.ForeColor = watermarkColor;
                }
            }
        }

        [Browsable(true)]
        public string Watermark
        {
            get 
            { 
                return watermark; 
            }
            set 
            {
                watermark = value;
                if (empty) 
                {
                    base.Text = watermark;
                    base.ForeColor = watermarkColor;
                }
            }
        }

        public WatermarkRichTextbox()
        {
            empty = true;
            foreColor = ForeColor;
        }

        [Browsable(true)]
        public new Color ForeColor 
        {
            get 
            {
                return foreColor; 
            }
              
            set 
            {
                foreColor = value;
                if (! empty)
                    base.ForeColor = value;
            }
        }

        public override string Text 
        {
            get
            {
                if (empty)
                    return "";
                return base.Text;
            }
            set 
            {
                if (value == "") 
                {
                    empty = true;
                    base.ForeColor = watermarkColor;
                    base.Text = watermark;
                } 
                else 
                    base.Text = value;
            }
        }

        protected override void OnGotFocus (EventArgs e) 
        {
            if (empty) 
            {
                empty = false;
                base.ForeColor = foreColor;
                base.Text = "";
            }
            
            base.OnGotFocus (e);
        }

        protected override void OnLostFocus (EventArgs e) 
        {
            base.OnLostFocus (e);
            if (base.Text == "") 
            {
                empty = true;
                base.ForeColor = watermarkColor;
                base.Text = watermark;
            } 
            else
                empty = false;
        }
    }
Credits to the original author/programmer for submitting this kind of solution.

2 comments:

  1. missing references

    ReplyDelete
    Replies
    1. I made a vb.net class with the references namely: System.Drawing and System.ComponentModel.

      See this link: http://dotnetgenetics.blogspot.com.au/2014/03/custom-richtextbox-control-with_2.html

      Delete