How do I specify the initial value of a textBox?

I want my C # program to have initial values ​​for its own textboxes

. For example, in one of them textboxes

he has to say "Please enter your name"

.

When you click (or tabStop) on textbox

, the initial value should disappear and the user can enter their input into textbox

.

I can do it all with help click_event

, but using this method, the original text will not have less opacity. How can I achieve this?

+2


source to share


4 answers


This is how I did it:



Boolean first_time_click = true;

private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.ForeColor = System.Drawing.Color.Gray;
            textBox1.Text = "Enter the Text";
        }

private void For_First_Click()
        {
            if (first_time_click)
            {
                textBox1.Clear();
                textBox1.ForeColor = textBox1.ForeColor = SystemColors.WindowText;
            }
            first_time_click = false;
        }

private void textBox1_Click(object sender, EventArgs e)
        {
            For_First_Click();
        }

      

+4


source


I assume you are talking about winform (tabstop), you should handle it in case of an event key pressed. you can use the below code:

TextBox1.Select(0, TextBox1.Text.Length);

      



this will select the text and the window will delete it for you as soon as the user starts typing

you can use the same code to have this behavior also for TabStop

0


source


All you have to do is set the Textbox.Text property and use the GotFocus event to clear the field when the user clicks on it (or tabs) to start typing.

Always remember that there are more ways than a mouse to navigate a form, so use the GotFocus event to detect when the user enters the control, and use the Validated event to determine when they changed data and exited the control.

0


source


You need a java script for this type of effect. Since the java script provides you with mouse and mouse functionality, these are functions that provide you with the same functionality as this search bar page provides. If you need a code, please answer me, I can give you.

-1


source







All Articles