Partager via


Comment : effectuer une détection en cas d'appui sur la touche Entrée

Cet exemple montre comment détecter quand la Enter touche est enfoncée sur le clavier.

Cet exemple se compose d’un fichier XAML (Extensible Application Markup Language) et d’un fichier code-behind.

Exemple

Lorsque l’utilisateur appuie sur la Enter touche, TextBoxl’entrée dans la zone de texte apparaît dans une autre zone de l’interface utilisateur.

Le code XAML suivant crée l’interface utilisateur, qui se compose d’un , d’un StackPanel, d’un TextBlocket d’un 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>

Le code suivant crée le gestionnaire d’événements KeyDown . Si la touche enfoncée est la Enter touche, un message s’affiche dans le TextBlockfichier .

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

Voir aussi