0

Not able to detect arrows key. I understand that it's the default behavior of datagridview cell for moving focus to next/previous/up/down. I googled a lot about this topic but didn't find the exact solution.
Here is my code.

private void dgvHP_OPDPrescriptionDiagnosis_Detail____EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    switch (dgvHP_OPDPrescriptionDiagnosis_Detail___.CurrentCell.ColumnIndex)
    {
        case 9:
            TextBox txtDiagnosis = e.Control as TextBox;
            if (txtDiagnosis != null)
                 txtDiagnosis.KeyDown += new KeyEventHandler(DiagnosisKeyDown);
            break;
    }
}

private void DiagnosisKeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.Down)
    {
         MessageBox.Show("Hi");
    }
}
wohlstad
  • 12,661
  • 10
  • 26
  • 39

1 Answers1

0

Hope it solve your purpose

Link : https://social.msdn.microsoft.com/Forums/en-US/a7e1bfa5-af91-4594-bc39-6b9000af95b4/how-to-detect-arrow-keys-in-a-textbox?forum=winforms

Make your own DataGridView control override the ProcessKeyPreview event as:

class dgv : DataGridView

    {

        protected override bool ProcessKeyPreview(ref Message m)

        {

            KeyEventArgs args1 = new KeyEventArgs(((Keys)((int)m.WParam)) | Control.ModifierKeys);

            switch (args1.KeyCode)

            {

                case Keys.Left:

                case Keys.Right:

                case Keys.Up:

                case Keys.Down:

                    return false;

            }

            return base.ProcessKeyPreview(ref m);

        }

    }

Use this inherited control in your project, you will able to make arrow keys work normally in the textbox.

Sund'er
  • 666
  • 1
  • 4
  • 11