Procedura: rilevare il momento in cui è stato premuto il tasto INVIO

In questo esempio viene illustrato come rilevare quando il Enter tasto viene premuto sulla tastiera.

Questo esempio è costituito da un file XAML (Extensible Application Markup Language) e da un file code-behind.

Esempio

Quando l'utente preme il Enter tasto in TextBox, l'input nella casella di testo viene visualizzato in un'altra area dell'interfaccia utente.

Il codice XAML seguente crea l'interfaccia utente, costituita da un StackPaneloggetto , TextBlocke da un oggetto TextBox.

<StackPanel>
  <TextBlock Width="300" Height="20">
    Type some text into the TextBox and press the Enter key.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1"
           KeyDown="OnKeyDownHandler"/>
  <TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>

Il code-behind seguente crea il KeyDown gestore eventi. Se il tasto premuto è il Enter tasto , viene visualizzato un messaggio in TextBlock.

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        textBlock1.Text = "You Entered: " + textBox1.Text;
    }
}
Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs)
    If (e.Key = Key.Return) Then
        textBlock1.Text = "You Entered: " + textBox1.Text
    End If
End Sub

Vedi anche