ARTICLE

How to use TextBox KeyDown event handler in WPF using VB.NET

Posted by Rohatash Kumar Articles | WPF using VB.NET February 02, 2011
In this article we will learn how to use TextBox KeyDown event handler in WPF using VB.NET.
Download Files:
 
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor