using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Windows.Forms; using System.Drawing; namespace WindowsFormsApplication2 { [DesignerCategory("Default")] class TextBoxEx : TextBox { public TextBoxEx() { Watermark = ""; } public string Watermark { get; set; } private bool ShowsWatermark { get { return !DesignMode && !Focused && Text == ""; } } private Color WatermarkColor { get { return SystemColors.GrayText; } } protected override void WndProc(ref Message m) { base.WndProc(ref m); const int WM_PAINT = 0x000f; switch (m.Msg) { case WM_PAINT: if (ShowsWatermark) { PrintWatermark(); } break; } } private void PrintWatermark() { using (var g = CreateGraphics()) { //using (var br = new SolidBrush(BackColor)) //{ // g.FillRectangle(br, this.ClientRectangle); //} using (var br = new SolidBrush(WatermarkColor)) { var sf = new StringFormat(); g.DrawString(Watermark, Font, br, ClientRectangle, sf); } } } } }