Strange behavior! when I click on the button on the metro app all works well, but when i click enter(the button on the KB) the only thing that happens is everything is cleared!
This fails
private void TextBox_KeyDown_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
textBlock.Text = textBox1.Text;
// textBox1.Text = "";
}
}
This works as expected
private void Send_Click(object sender, RoutedEventArgs e)
{
textBlock.Text = textBox1.Text;
textBox1.Text = "";
}
What am I doing wrong ?
thanks
It would be best if when pressing Enter on the textbox, it will actualy simulate a click on the button, to assure that both actions are really the same.
Also, as Chris mentioned, you shouldn’t really handle the KeyDown even in this case: you can set the AcceptButton property of the form to be the Send button, which means that when pressing Enter, the button will be pressed – even if not focused. This sort of problem is a good example of why using the AcceptButton property.