Jon,
as far as I know there is no way you can override the painting the a TextBox control, short of drawing everything yourself.
There is a solution that should do the trick for you, though. You will need to sublcass the TextBox and override a few things. Consider the following code I put together (far from thouroughly tested and polished):
public partial class WatermarkBox : TextBox {
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 WatermarkBox () {
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;
}
}
In a nutshell it does what you were trying to do, but it shouldn't mess the Text property for the other users.
A few comments...
Wenow havetwo brand new properties, Watermark and WatermarkColor that will appear in the designer properties thanks to the Browsable attribute (you can improve on them by setting the category and other amenities). These are obviously the text we want to display as a watermark and the color the watermark should appear in.
The properties Text and ForeColor were also re-declared, but they were not overridden, but hidden. This is due to the fact that the painting functions would have seen our overridden version, and so we would have been unable to report the correct values to functions inspecting the Text or the ForeColor property.
The rest is quite simple. The control will be cleared when it gets the focus and will eventually restore the watermark, if needed, when the focus is lost.
HTH
--mc