Cara: Mendeteksi Saat Tombol Enter Ditekan
Contoh ini menunjukkan cara mendeteksi kapan tombol Enter ditekan pada keyboard.
Contoh ini terdiri dari file Extensible Application Markup Language (XAML) dan file code-behind.
Contoh
Saat pengguna menekan Enter tombol di TextBox, input dalam kotak teks muncul di area lain antarmuka pengguna (UI).
XAML berikut membuat antarmuka pengguna, yang terdiri dari StackPanel, TextBlock, dan TextBox.
<StackPanel>
<TextBlock Width="300" Height="20" Text="Type some text into the TextBox and press the Enter key." />
<TextBox Width="300" Height="30" Name="textBox1" KeyDown="textBox1_KeyDown" />
<TextBlock Width="300" Height="100" Name="textBlock1" />
</StackPanel>
Kode berikut di belakang membuat KeyDown penanganan aktivitas. Jika tombol yang ditekan adalah Enter kunci, pesan ditampilkan di TextBlock.
private void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
textBlock1.Text = $"You Entered: {textBox1.Text}";
}
}
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs)
If e.Key = Key.Return Then
textBlock1.Text = "You Entered: " + textBox1.Text
End If
End Sub
Baca juga
Berkolaborasi dengan kami di GitHub
Sumber untuk konten ini dapat ditemukan di GitHub, yang juga dapat Anda gunakan untuk membuat dan meninjau masalah dan menarik permintaan. Untuk informasi selengkapnya, lihat panduan kontributor kami.
.NET Desktop feedback