0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;



namespace HelloWorld
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string filePathTEST = "";



        public MainWindow()
        {
            InitializeComponent();
            // Clearing text event handlers
            textBox1.GotFocus += textBox1_GotFocus;


            // Enter event handlers for textboxes
            textBox1.KeyDown += new System.Windows.Input.KeyEventHandler(textBox1_KeyDown);



        }

 static void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //enter key is down
            }
        }

The error I am getting when I try to run the above code is the following:

Cannot implicitly convert type 'System.Windows.Forms.KeyEventHandler' to 'System.Windows.Input.KeyEventHandler'

Then I tried changing the code to System.Windows.Input and then I get the following:

Error 1 'System.Windows.Input.KeyEventArgs' does not contain a definition for 'KeyCode' and no extension method 'KeyCode' accepting a first argument of type 'System.Windows.Input.KeyEventArgs' could be found (are you missing a using directive or an assembly reference?)

The whole point of me doing this is that when I press enter on a textbox, I want to take the text in that textbox and populate a certain text file with it but I am not sure how to go about doing that.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
user2635911
  • 472
  • 2
  • 6
  • 16

2 Answers2

1

The compiler thinks you mean to use 'System.Windows.Forms.KeyEventHandler' due to the namespace you've added: System.Windows.Forms.

Remove this line and your code should work:

using System.Windows.Forms;

Second, you should use Key instead of KeyCode since that is the WPF variant:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
         //
    }
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

You're running foul of the rather confused state of Windows client development. System.Windows.Forms is part of the WinForms library, the original UI framework that shipped with .NET 1.0. The project you're working with is written in WPF (Windows Presentation Framework) the new lib that started shipping in .NET 3. There's a lot of overlap, but the two libraries are distinctive.

The class you want is System.Windows.Input.KeyEventArgs. If you go up onto MSDN for the documentation for this class, you'll see that it does NOT in fact have a KeyCode property. It does, however, have a Key property which should do what you want.

I'll leave actually finding and reading the MSDN documentation as an exercise for the reader. :-)

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63