WPF Use TextBox KeyDown event handler in VB.NET

In this article we will learn how to use TextBox KeyDown event handler in WPF using VB.NET.
  • 5889
Download Files:
 

In this article we will learn how to use TextBox KeyDown event handler in WPF using VB.NET.

KeyDown event

KeyDown event fires when we enter the Key of the keyboard. In WPF TextBox control We use the KeyDown Event and the Key property of e the KeyEventArgs, comparing it to Key.

For example

Drag and drop two TextBlock one TextBox on the form.

k1.gif

Figure 1.

XAML code

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">

    <StackPanel>

        <TextBlock Width="300" Height="20" Background="Aqua" Foreground="Maroon">

      Type some text into the TextBox and press the Enter key.

        </TextBlock>

        <TextBox Width="300" Height="30" Name="textBox1"

             KeyDown="OnKeyDownHandler" Background="DarkGreen" Foreground="MediumOrchid" />

        <TextBlock Width="300" Height="75" Name="textBlock1" Background="DarkOrange" />

    </StackPanel>

</Window>

Now select textBox control and Press F4 for KeyDown event.

k2.gif

Figure 2.

Now double click on the KeyDown event and add the following code.

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

 

we can also set integer number according to key.

 

Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs)

        If e.Key = 6 Then

            textBlock1.Text = "You Entered: " + textBox1.Text

        End If

    End Sub

 

Now run the application and test it.
 

k3.gif 

Figure 3.

 

Now enter some text into the TextBox.
 

k4.gif 

Figure 4.

 

Now press enter key.
 

k5.gif 

Figure 5.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.