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.
Credits to the original author/programmer for submitting this kind of solution.
/// <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; } }
missing references
ReplyDeleteI made a vb.net class with the references namely: System.Drawing and System.ComponentModel.
DeleteSee this link: http://dotnetgenetics.blogspot.com.au/2014/03/custom-richtextbox-control-with_2.html