0

My original intention in to make enter event for text box to run btnOK_Click event, but after several try I can't make it happen, so I tried another way and try KeyPress for any key but still didn't work, so I made these two simple code, but it still didn't work either;

    private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            //enter key is down
            //btnOK_Click(this, e);
            System.Windows.Forms.MessageBox.Show("My message here");
        }
    }
    private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Return)
        {
            //enter key is down
            //btnOK_Click(this, e);
            System.Windows.Forms.MessageBox.Show(((char)Keys.Return).ToString());
        }
    }

Any suggestion? I read some similar questions and they said to set the IsInputKey property to true but I can't find it anywhere. I use Visual Studio 2008

RJFalconer
  • 10,890
  • 5
  • 51
  • 66
Darjeeling
  • 959
  • 5
  • 19
  • 40
  • IsInputKey is in [Control](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.isinputkey%28v=vs.110%29.aspx). See http://stackoverflow.com/questions/1298640/c-sharp-trying-to-capture-the-keydown-event-on-a-form – RJFalconer Apr 29 '15 at 13:19

3 Answers3

0

It seems to me that you are looking to something like this

private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape)//should be replaced with enter
    {
        button1.PerformClick();
    }
}

NOTE: the code above is on the KeyDown instead of the KeyPress

This code should work, assuming you are using winforms

maam27
  • 444
  • 3
  • 21
0

Two Options : 1) Use Key Up Event As

public void txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOK_Click(sender, e); // or btn.PerformClick();
                return;               
            }
        }

2) Make BtnOK the AcceptButton of the form. (Note : this will be for all textboxes in your form)

this.AcceptButton = btnOK;
Abdul Rehman Sayed
  • 6,532
  • 7
  • 45
  • 74
0

use Escape key instead of return key:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOK_Click(sender, e);
                MessageBox.Show("My message here");
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnOK_Click(sender, e);
                MessageBox.Show(((char)Keys.Escape).ToString());
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test");
        }
    }
}

also you can check both keys in KeyDown event. you can also use

btnOK.PerformClick();

instead of

btnOK_Click(sender, e);
Masoud Mohammadi
  • 1,721
  • 1
  • 23
  • 41