WPF equivalent of Winform's KeyEventArgs - Invoking a KeyDown-event with a specific key

1.8k views Asked by At

How to convert the below code that works with Winforms to make it work with WPF:

  textBox1.Text = (input); 
  KeyEventArgs ev = new KeyEventArgs(Keys.Enter);
  textBox1_KeyDown(sender, ev);

I want to invoke the KeyDown event with a specific key so that a specific value gets entered into textBox1 automatically.

1

There are 1 answers

0
oRole On BEST ANSWER

To subscribe to the KeyDown-event of a TextBox using WPF to enter some random text to it when a specific key is pressed, you can use one of the following methods:

Method 1 - Define the event using XAML (you do not need to subscribe manually to the event):

XAML:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

        <!-- define your TextBox and the KeyDown-event here -->
        <TextBox x:Name="MyTextBox" Width="120" Height="30" KeyDown="MyTextBox_KeyDown"/>

    </Grid>
</Window>

C#-Code:

using System.Windows;
using System.Windows.Input;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
        { 
            // if pressed key is "Enter", do something
            if (e.Key == Key.Enter)
            {
                this.MyTextBox.Text = "Some Text!";
            }
        }
    }
}

Method 2 - Define the event and subscribe to it using Code:

XAML:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

        <!-- define your TextBox here -->
        <TextBox x:Name="MyTextBox" Width="120" Height="30"/>

    </Grid>
</Window>

C#-Code:

using System.Windows;
using System.Windows.Input;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.MyTextBox.KeyDown += this.MyTextBox_KeyDown;
        }

        private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
        { 
            // if pressed key is "Enter", do something
            if (e.Key == Key.Enter)
            {
                this.MyTextBox.Text = "Some Text!";
            }
        }
    }
}

To invoke the KeyDown-event with a specific key (here: Key.Enter) you can use:

using System;
using System.Windows.Interop;

KeyEventArgs enterPressedArgs = new KeyEventArgs(Keyboard.PrimaryDevice, new HwndSource(0, 0, 0, 0, 0, "", IntPtr.Zero), 0, Key.Enter);
this.MyTextBox_KeyDown(null, enterPressedArgs);