-5

I want to make text appear in the text box but dissapear when the text box is clicked.

I want it to be like this: https://gyazo.com/17087686454b0b884104dcf08c551de0

That is basically all I want.

EDIT: OP has indicated this is a winforms app

Terry
  • 1
  • 2
  • @JohnLord incorrect. Also it's a bad assumption. OP may want to just replicate the behavior in a different technology. In this case he did - _WinForms_ –  Jun 14 '19 at 04:17
  • @Terry Are you sure this is winforms and not the web? I am surprised given the answer you selected. – mjwills Jun 14 '19 at 04:34
  • he didn't specify winforms originally and that's abnormal behavior for a winforms app. – John Lord Jun 16 '19 at 19:35

2 Answers2

0

There is no direct method to add a placeholder to a text box. But you can do something like this easily.

Textbox newTextBox = new Textbox();
newTextBox.Text = "Search google or type URL";

newTextBox.GotFocus += GotFocus.EventHandle(RemoveText);
newTextBox.LostFocus += LostFocus.EventHandle(AddText);

public void RemoveText(object sender, EventArgs e)
{
    if (newTextBox.Text == "Search google or type URL") 
    {
     newTextBox.Text = "";
    }
}

public void AddText(object sender, EventArgs e)
{
    if (string.IsNullOrWhiteSpace(newTextBox.Text))
        newTextBox.Text = "Search google or type URL";
}
-1

Use placeholder attribute should do it

Ken Lang
  • 92
  • 4
  • What’s.a placeholder attribute? –  Jun 14 '19 at 03:35
  • Are you using HTML /web or a Windows application? – Ken Lang Jun 14 '19 at 03:47
  • Placeholder attribute is on a test input element in HTML. If you want it to have text when the user opens the page you put the text you want in it. Just Google it. Also being new here it ticks me off someone would downvote my answer when it's not wrong! – Ken Lang Jun 14 '19 at 03:51
  • _"Placeholder attribute is on a test input element in HTML"_ - prior to the OP's feedback, what made you think this was a HTML question? Normally you shouldn't answer a question that isn't clearly defined. [ask]. –  Jun 14 '19 at 04:07
  • OP has now indicated that it is a **WinForms** app so your answer doesn't apply –  Jun 14 '19 at 04:11
  • I had asked later for clarification, I don't have enough achievement to comment – Ken Lang Jun 14 '19 at 06:42