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.

1

3 Answers 3

4

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

see Watermark in System.Windows.Forms.TextBox

Sign up to request clarification or add additional context in comments.

3 Comments

Either you found the same site in a google search, or you are copying a reference made on a previous SO answer. If the latter, make sure to cite the author's post in your answer.
@BradChristie good point. I flagged this q with that post, might as well post it here too.
Also, I here's another one using WndProc (If author doesn't mind that).
2

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

Comments

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;
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.