11

My Program: Has one textbox only. I am writing code using C# Language.

My Aim: To display text/watermark in textbox: 'Please enter your name'. So, when user clicks on the textbox, the default text/watermark gets clear/deleted so that user can enter his name in the textbox.

My problem: I tried various codes that are available online but none of them seem to work for me. So, I thought I should ask here for a simple code. I have found a code online but that doesn't seem to work:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetWatermark("Enter a text here...");
        }

        private void SetWatermark(string watermark)
        {
            textBox1.Watermark = watermark;
        }
    }
}

Error:

Error 1 'System.Windows.Forms.TextBox' does not contain a definition for 'Watermark' and no extension method 'Watermark' accepting a first argument of type 'System.Windows.Forms.TextBox' could be found (are you missing a using directive or an assembly reference?)

Please, if you have any other suggestions for what I am aiming for, I would really appreciate it. I tired many examples online but all are confusing/don't work. Thanks for your help in advance. :)

Smith
  • 387
  • 3
  • 6
  • 14
  • there is no watermark for a textbox. You could create your own by greying out the text, then making it black if they put something in it – Jonesopolis Aug 28 '13 at 20:01
  • You can always set the foreground to WhiteSmoke or something and then hook into the GotFocus event to clear/change color. – DanteTheEgregore Aug 28 '13 at 20:05

1 Answers1

36

just tried this out. It seems to work fine in a new Windows Forms project.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        textBox1.ForeColor = SystemColors.GrayText;
        textBox1.Text = "Please Enter Your Name";
        this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
        this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
    }

    private void textBox1_Leave(object sender, EventArgs e)
    {
        if (textBox1.Text.Length == 0)
        {
            textBox1.Text = "Please Enter Your Name";
            textBox1.ForeColor = SystemColors.GrayText;
        }
    }

    private void textBox1_Enter(object sender, EventArgs e)
    {
        if (textBox1.Text == "Please Enter Your Name")
        {
            textBox1.Text = "";
            textBox1.ForeColor = SystemColors.WindowText;
        }
    }
}
Nikos C.
  • 50,738
  • 9
  • 71
  • 96
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • Awesome! To the point! Thanks a lot Jonesy! :) – Smith Aug 28 '13 at 20:18
  • 4
    Seems you're *Reinventing the wheel*. Why not use `EM_SETCUEBANNER` – Sriram Sakthivel Aug 28 '13 at 20:19
  • 2
    I don't think this answer would work well with DataBinding. – LarsTech Aug 28 '13 at 20:20
  • 1
    I get this may not be ideal, but clearly Smith is newer to C#, and a simple solution may be better than delving into DataBinding and importing DLLs – Jonesopolis Aug 28 '13 at 20:27
  • Yes. I just wanted a simple program for demonstration and better understanding and this does the job. Thanks again. – Smith Aug 28 '13 at 20:35
  • That worked nicely. But I changed your code to use the system colors, because otherwise this produces wrong results if the user has changed the Windows color theme. – Nikos C. Mar 26 '15 at 17:04
  • 2
    I created a small GitHub repository showing all methods to create a watermark. https://github.com/akorb/PlaceholderTextBox – Andy Jun 10 '15 at 13:43
  • What about drag and drop. I don't believe it would be compatible either. – Szybki Mar 10 '16 at 11:34
  • Nice solution! I would query on the textbox color for the enter event instead of a literal string. That way the watermark string itself can be used as a valid entry . Also, it is much simpler to code if you are handling the same event for multiple text box entries. That way you can store the watermark text in the Tag property and inject the Tag property as text in the Leave event. – Optavius Oct 07 '16 at 10:42