TextBox PreviewKeyDown, PreviewKeyUp, PreviewTextInput, KeyDown, KeyUp and TextChanged events : TextBox Action « Windows Presentation Foundation « C# / CSharp Tutorial

<Window x:Class="RoutedEvents.KeyPressEvents"
    Title="KeyPressEvents" Height="400" Width="400" >
          <DockPanel Margin="5">
            <TextBlock Margin="3" >Type here:</TextBlock>
            <TextBox PreviewKeyDown="KeyEvent" KeyDown="KeyEvent" 
                     PreviewKeyUp="KeyEvent" KeyUp="KeyEvent"
      <ListBox Margin="5" Name="lstMessages"></ListBox>
      <CheckBox Margin="5" Name="chkIgnoreRepeat">Ignore Repeated Keys</CheckBox>
      <Button Click="cmdClear_Click" HorizontalAlignment="Right">Clear List</Button>

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RoutedEvents
    public partial class KeyPressEvents : System.Windows.Window
        public KeyPressEvents()
        private void KeyEvent(object sender, KeyEventArgs e)
            if ((bool)chkIgnoreRepeat.IsChecked && e.IsRepeat) return;
            string message = "Event: " + e.RoutedEvent + " " + " Key: " + e.Key;

        private void TextInput(object sender, TextCompositionEventArgs e)
            string message = "Event: " + e.RoutedEvent + " " + " Text: " + e.Text;

        private void TextChanged(object sender, TextChangedEventArgs e)
            string message = "Event: " + e.RoutedEvent;

        private void cmdClear_Click(object sender, RoutedEventArgs e)
WPF Text Box Preview Key Down Preview Key Up Preview Text Input Key Down Key Up And Text Changed Events

24.14.TextBox Action
24.14.1.React to TextBox focus eventReact to TextBox focus event
24.14.2.TextBox MouseLeftButtonDown action and PreviewMouseLeftButtonDown actionTextBox MouseLeftButtonDown action and PreviewMouseLeftButtonDown action
24.14.3.Listen to TextBox text changed eventListen to TextBox text changed event
24.14.4.Handler for the PreviewKeyDown event on the TextBoxHandler for the PreviewKeyDown event on the TextBox
24.14.5.TextBox PreviewKeyDownTextBox PreviewKeyDown
24.14.6.TextBox KeyDown actionTextBox KeyDown action
24.14.7.TextBox PreviewKeyUpTextBox PreviewKeyUp
24.14.8.TextBox KeyUpTextBox KeyUp
24.14.9.TextBox TextInput eventTextBox TextInput event
24.14.10.TextBox PreviewTextInputTextBox PreviewTextInput
24.14.11.TextBox PreviewKeyDown, PreviewKeyUp, PreviewTextInput, KeyDown, KeyUp and TextChanged eventsTextBox PreviewKeyDown, PreviewKeyUp, PreviewTextInput, KeyDown, KeyUp and TextChanged events