I'm trying to delete the content of a TextBox when the backspace key is pressed, but it is not working. The code:
private void txtConteudo_TextChanged(object sender, TextChangedEventArgs e)
{
if(Keyboard.IsKeyDown(Key.Back))
{
txtConteudo.Text = "";
}
}
The xaml of the textbox:
<TextBox x:Name="txtConteudo" Text="0" FontSize="16" IsReadOnly="True" Margin="10,5,16,139" TextChanged="txtConteudo_TextChanged" />
You want to use the PreviewKeyDown event instead. Try changing your current code to:
Code:
private void txtConteudo_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.IsKeyDown(Key.Back))
{
txtConteudo.Text = "";
}
}
Xaml:
<TextBox x:Name="txtConteudo" Text="0" FontSize="16" IsReadOnly="True" Margin="10,5,16,139" PreviewKeyDown="txtConteudo_PreviewKeyDown" />
First of all, you shouldn't use textchanged event for that. Instead use KeyDown event
private void txtConteudo_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Key.Back)
{
txtConteudo.Text = "";
}
}
Try this
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 8)
{
textBox1.Text = "";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.