2

I have a textbox in my form. On the form load event, i want my textbox.text = "Enter Name" but it should appear as if the textbox is clicked, the text of the textbox gets disappeared. Attached image will help you understanding my question :)

First Image is what i actually want to do.

Abid Ali
  • 1,731
  • 8
  • 26
  • 62
  • possible duplicate of [Watermark System.Windows.Forms.TextBox using C#](http://stackoverflow.com/questions/578193/watermark-system-windows-forms-textbox-using-c-sharp) – Caleb Dec 20 '11 at 04:38

3 Answers3

4

http://vidmar.net/weblog/archive/2008/11/05/watermarked-textbox-in-windows-forms-on-.net.aspx

see Watermark in System.Windows.Forms.TextBox

Community
  • 1
  • 1
William Melani
  • 4,270
  • 1
  • 21
  • 44
  • 1
    Either you found the same site in a google search, or you are copying a reference made on a [previous SO answer](http://stackoverflow.com/questions/578193/watermark-system-windows-forms-textbox-using-c-sharp). If the latter, make sure to cite the author's post in your answer. – Brad Christie Dec 19 '11 at 20:00
  • @BradChristie good point. I flagged this q with that post, might as well post it here too. – William Melani Dec 19 '11 at 20:02
  • Also, I here's another one [using WndProc](http://stackoverflow.com/questions/2539903/sendmessage-vs-wndproc) (If author doesn't mind that). – Brad Christie Dec 19 '11 at 20:04
2

Use TextBox.ForeColor and change it to gray and Enter/Leave events of textBox to change the color and remove the text

ahmadali shafiee
  • 4,350
  • 12
  • 56
  • 91
1

here is a solution without DllImport usage

/// <summary>
/// inspired by this forum entry: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/10f75954-6d14-4926-a02d-98649653b9c8/
/// Watermark TextBox in winform
/// </summary>
public class WatermarkTextBox : TextBox
{
  private string watermarkText;
  private Color watermarkColor;
  private Color foreColor;
  private bool isTextBoxEmpty;

  public WatermarkTextBox()
  {
    this.WatermarkText = "Watermark Text...";
    this.WatermarkColor = Color.FromKnownColor(KnownColor.Silver);
    this.Enter += onEnter;
    this.Leave += onLeave;
  }

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

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

  [Browsable(true)]
  public string WatermarkText
  {
    get { return this.watermarkText; }
    set
    {
      if (value == this.watermarkText)
      {
        return;
      }
      this.watermarkText = value;
      if (base.Text.Length == 0)
      {
        this.isTextBoxEmpty = true;
        base.Text = this.watermarkText;
        base.ForeColor = this.watermarkColor;
      }
      this.Invalidate();
    }
  }

  public override string Text
  {
    get { return this.isTextBoxEmpty ? string.Empty : base.Text; }
    set
    {
      if (string.IsNullOrEmpty(value))
      {
        this.isTextBoxEmpty = true;
        base.ForeColor = this.watermarkColor;
        base.Text = this.watermarkText;
      }
      else
      {
        this.isTextBoxEmpty = false;
        base.ForeColor = this.foreColor;
        base.Text = value;
      }
    }
  }

  private void onEnter(object sender, EventArgs e)
  {
    if (this.isTextBoxEmpty)
    {
      this.isTextBoxEmpty = false;
      base.ForeColor = this.foreColor;
      base.Text = string.Empty;
    }
  }

  private void onLeave(object sender, EventArgs e)
  {
    if (string.IsNullOrEmpty(base.Text))
    {
      this.isTextBoxEmpty = true;
      base.ForeColor = this.watermarkColor;
      base.Text = this.watermarkText;
    }
    else
    {
      this.isTextBoxEmpty = false;
    }
  }
}
punker76
  • 14,326
  • 5
  • 58
  • 96