0

What i want to do is:

  1. When the user start typing anything in the textBox2 first make it empty and then start showing what the user is typing.

  2. Then if the user deleting what he was typing then show the original text again.

Now what it does is that i need to delete on my own the text inside but then when i try to type anything i see the original text again since it's empty.

private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text != "" || textBox2.Text == "Enter Email Account")
            {
                textBox2.Text = "Enter Email Account";
                button2.Enabled = false;
            }
            else
            {
                button2.Enabled = true;
            }
        }
  • 1
    What you actually want to do is called `watermark`, use this keyword to search for answers. e.g. http://stackoverflow.com/questions/4902565/watermark-textbox-in-winforms – Eric Dec 07 '15 at 01:26
  • 2
    Actually it is called CueText. Watermark is what the poster in that link called it. – Ňɏssa Pøngjǣrdenlarp Dec 07 '15 at 01:34

1 Answers1

1

You should be using a Watermark. But in case you wish to do it as you started, you set the default value in properties. You need first of all to create your event handlers during the Enter and Leave event;

private void Form1_Load()
    {
        textBox1.Text = "Enter Email Account";
        textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
        textBox1.LostFocus += new EventHandler(textBox1_Leave);
    }

Then on Enter, the watermark text should be deleted;

protected void textBox1_GotFocus(object sender, EventArgs e)
    {
        if (textBox1.Text == "Enter Email Account")
        { 
            textBox1.Text = "";

        }
    }

Then you re-check on Leave event;

protected  void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            { 
                textBox1.Text = "Enter Email Account";
            }
         }
Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61