Search code examples
c#plsqltextboxruntime

How to make textbox text disappear when I click on it


I managed to create textboxes dynamically in C#. When a textbox has text, can I make it disappear when I click on it?

I need to put a word inside textboxes that is the result of a select in Oracle.


Solution

  • Assign a value to the Text property of the TextBox. You could subscribe then to the GotFocus event, and set the text value to an empty string.

    // Holds a value determining if this is the first time the box has been clicked
    // So that the text value is not always wiped out.
    bool hasBeenClicked = false;
    
    private void TextBox_Focus(object sender, RoutedEventArgs e)
    {
        if (!hasBeenClicked)
        {
            TextBox box = sender as TextBox;
            box.Text = String.Empty;
            hasBeenClicked = true;
        }
    }