Click here to Skip to main content
15,068,988 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
i have a textbox in windows form
now i want a default text on textbox like
we have place holder in html
Posted
Updated 16-Sep-21 8:13am
Comments
Aydin Homay 2-Dec-13 6:48am
   
Hi please ask you question clearly and why you don`t user Richtextbox ?

If you want ready to go solution, check this out:

Custom TextBox with Watermark[^]
   
C#
private void Form_load(object sender, EventArgs e)
{
    textBox.Text = "Place Holder text..."
}

private void textBox_Enter(object sender, EventArgs e)
{
    if(textBox.Text == "Place Holder text...")
    {
        textBox.Text = ""
    }
}

private void textBox_Leave(object sender, EventArgs e)
{
    if(textBox.Text == "")
    {
        textBox.Text = "Place Holder text..."
    {
}


This places text in the textbox on form load. when focus is placed on the textbox and the current text of the textbox(before focus) is the text of the placeHolder, it clears the text.
If focus leaves the textbox and no text is inserted the text changes back to the placeHolder text.
The check on the placeHolder text when focus is gained on the textbox is to ensure that previously user input text is not removed.

This should give you some idea of how you might want to do it

If this is not what you had in mind
Take a look at this
TextBox with Placeholder[^]
it might be more to what you are looking for
   
v2
Refer- Adding placeholder text to textbox[^].
Quote:

Wouldn't that just be something like this:


C#
Textbox myTxtbx = new Textbox();
myTxtbx.Text = "Enter text here...";

myTxtbx.OnFocus += OnFocus.EventHandle(RemoveText);
myTxtbx.LoseFocus += LoseFocus.EventHandle(AddText);

public RemoveText(object sender, EventArgs e)
{
     myTxtbx.Text = "";
}

public AddText(object sender, EventArgs e)
{
     if(myTxtbx.Text == "")
        myTxtbx.Text = "Enter text here...";
}

Thats just psuedocode but the concept is there.


in VB
textbox1 is the textbox server control

textbox1.Attributes.Clear()
textbox1.Attributes.Add("Placeholder", "text of placeholder")
   
Comments
CHill60 17-Sep-21 4:06am
   
Firstly, the question was about C# not VB
There is no such thing as a "textbox server control" in a Windows Form, that is a WebControl / ASP.NET
Similarly the WinForm TextBox control does not have an Attributes property
Finally, this 8 year-old question already has 3 appropriate answers, 2 of which were accepted as the "Best Solution".
You are late to the game and completely off-topic

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900