0

I have a simple requirment in C# WPF. I got a textbox in which I will enter some text. When I press Enter, I want an event to be fired. My code is

 private void AddKeyword(object sender, KeyEventArgs e)
  {
     if(e.Key == Key.Enter)
      {
         //DO something
      }
  } 

I have set my Textbox AcceptReturn =True; The Method is simply not working and I dont see any event fired when I press Enter. Please Help me out. Thanks in Advance

Vasanth91
  • 159
  • 1
  • 4
  • 11

1 Answers1

2

This work fine for me

<Window x:Class="WpfApplication1.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">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="190" KeyUp="textBox1_KeyUp" />
    </Grid>
</Window>

and

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

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                MessageBox.Show("Enter Fired!");
            }
        }
    }
}
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110