There is one possible pitfall with using textBoxName.Text = string.Empty;
and that is if you are using Text binding for your TextBox (i.e. <TextBox Text="{Binding Path=Description}"></TextBox>
). In this case, setting an empty string will actually override and break your binding.
To prevent this behavior you have to use the Clear method:
textBoxName.Clear();
This way the TextBox will be cleared, but the binding will be kept intact.
You wouldn't have to put it in the button click hander. If you were, then you'd assign your text box a name (x:Name
) in your view and then use the generated member of the same name in the code behind to set the Text
property.
If you were avoiding code behind, then you would investigate the MVVM design pattern and data binding, and bind a property on your view model to the text box's Text
property.
When you run your form and you want showing text in textbox is clear so you put the code : -
textBox1.text = String.Empty;
Where textBox1 is your textbox name.
You can use Any of the statement given below to clear the text of the text box on button click:
textBoxName.Text = string.Empty;
textBoxName.Clear();
textBoxName.Text = "";
For example:
XAML:
<Button Content="ok" Click="Button_Click"/>
<TextBlock Name="textBoxName"/>
In code:
private void Button_Click(object sender, RoutedEventArgs e)
{
textBoxName.Text = "";
}
I use this. I think this is the simpliest way to do it:
textBoxName.Clear();
For me texBoxName.Clear()
;is the best method because I have textboxs in binding and if I use other methods I do not have a good day
Source: Stackoverflow.com