Click here to Skip to main content
15,885,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi sir, i am working with window application, i need a text box to ask the user to enter your name, it should be written in text box with lite color, when user clicks on text box the mater should disappear and the user should able to write like normal text. please help me.
Posted
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 18:54pm    
WPF, Forms, ASP.NET, what? The answer depends on it. Tag it!
--SA

Drag and drop a new TextBox on your form. Go to the textbox's properties and set Text as:
Please Enter Name...

Change the ForeColor property and set the color you want.

Click on the events icon and add the following two events:
Enter, Leave.

Use the following code on each event:

private void txt_YourTextBox_Enter(object sender, EventArgs e)
{
   if (txt_YourTextBox.Text == "Please Enter Name...")
   {
      txt_YourTextBox.ForeColor = Color.Black;
      txt_YourTextBox.Text = "";
   }
}

private void txt_YourTextBox_Leave(object sender, EventArgs e)
{
   if (txt_YourTextBox.Text.Length == 0)
   {
      txt_YourTextBox.ForeColor = Color.BlueViolet;
      txt_YourTextBox.Text = "Please Enter Name...";
   }
}
 
Share this answer
 
v3
Comments
manoharnch 3-Aug-11 4:13am    
Good sir but the text is selecting
UJimbo 3-Aug-11 4:41am    
Had a small text mismatch. Added Fore color change on both cases
DanHodgson88 3-Aug-11 4:49am    
what do you mean by the text is selecting?
Hi,

This is the solution,

you have to change the textbox fore color in the form load

C#
textBox1.ForeColor = Color.Black;



and you have to write this code in the text box mouse click event


C#
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
    textBox1.Text = "";
    textBox1.ForeColor = Color.Black;
}


Try this :)

Thanks.
 
Share this answer
 
Comments
Soft009 3-Aug-11 4:04am    
First you have to give a text to the text box....
manoharnch 3-Aug-11 4:04am    
But we cont type the text again......
Ex: enter your name
He cont enter the name. :(
You can do it using the onFocus and lostFocus event.

When the TextBox gets the focus, you can clean the textbox, and when it lost, you can paint if needed.
 
Share this answer
 
Comments
manoharnch 3-Aug-11 4:07am    
Can u give me the code plz.....
Astolf 3-Aug-11 4:13am    
It's the same that UJimbo writes.
Try something like this:

private void window_Loaded(object sender, RoutedEventArgs e)
{
textbox1.Text = "Enter Name...";
}

private void textBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
textbox1.Text = "";
}


then add your fore color bits and bobs and sorted!

Hope this helps
 
Share this answer
 
Hi
first Go to the textbox's properties and set Text as:

C#
Enter your Name:


C#
private void Form1_Load(object sender, EventArgs e)
        {
textBox1.foreColor = Color.LightGray;
}

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
if(textBox1.Text == "Enter your Name:")
{
            textBox1.Text = "";
            textBox1.ForeColor = Color.Black;
}
        }



Hope this will help you

thanx.
 
Share this answer
 
Change color in the handler of the event TextChanged. Further details depends on the UI library you want to use. You did not specify it; see my comment to the question.

—SA
 
Share this answer
 
There is more to consider when creating such a scenario... an aspect you may not have considered yet would be validation. If you will be saving the input somewhere, do you plan to see if the user entered a name? Are you going to simply check for the prompt "Enter your name:" and dismiss that input?

The enter, leave, lostfocus etc events are fine to control what the textbox says and in what foreground color... but to make it really effective, especially if you're going to have many boxes like this, you should create your own control that handles the prompting and provides a validation method to the UI.

Just my .02 on the subject... as far as code, others have given you basic code to toggle text and forecolor... the rest is best left to existing tutorials on custom controls.

Best of luck!

Found one of the aritcles...

Prompted Textbox
[^]
 
Share this answer
 
v5

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