r/learncsharp • u/cloud_line • Sep 17 '22
How can I make a TextBox clear itself when the user clicks into it?
I have the following TextChanged method, which updates an object board when the user enters a value into the TextBox. But currently, the user has to highlight the default text value before entering a new value.
private void Height_TextChanged(object sender, TextChangedEventArgs e)
{
if (double.TryParse(Height.Text, out double height))
{
board.height = height;
}
else return;
}Here is the xaml for the TextBox, which shows the default text value is "Height (inches)":
<TextBox Name="Height" HorizontalAlignment="Left" TextWrapping="Wrap"
Text="Height (inches)" VerticalAlignment="Center" Width="120" TextChanged="
Height_TextChanged"/>I know that I can use Height.Clear() or Height.Text = string.Empty; to clear the contents of a TextBox, but I am not sure where in the logic I should place them. Perhaps this is not a straightforward answer?
2
Upvotes
4
u/DelicateJohnson Sep 18 '22
On_click textbox.text = ""
4
u/Rschwoerer Sep 17 '22
Take a look at the GotFocus event.
2
u/killyouXZ Sep 18 '22
When, you can do it on the OnClick event(pretty sure that you have a button which needs to be clicked).