Mar Tin
Mar Tin

Reputation: 2392

Capture EnterKey in DataGridView EditingControl

I need to capture the EnterKey in a DataGridView EditingControl:

enter image description here

When I hit Enter only the PreviewKeyDown event will detect it. But how can I capture this InputKey to prevent that the next row get selected?

Minimal example:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Tools.Tester
{
    class Stackoverflow
    {
        public static void QuestionCaptureFunctionalKeys()
        {
            Form form = new Form();

            //CREATE TABLE
            DataGridView table = new DataGridView();
            table.Location = new Point(40, 40);
            table.Columns.Add("Column 1", "Column 1");
            table.Rows.Add();
            form.Controls.Add(table);

            //ADD EVENT LISTENER
            TextBoxBase tb = null;
            table.EditingControlShowing += (s, e) =>
            {
                tb = e.Control as TextBoxBase;
                tb.PreviewKeyDown += eventKeyPreview;           //CHECK OUT KEY PREVIEW
                tb.KeyPress += eventKeyPress;                   //CHECK OUT KEY PRESS
                tb.KeyDown += eventKeyDown;                     //CHECK OUT KEY DOWN
            };

            table.CellEndEdit += (s, e) =>
            {
                if (tb == null) return;
                tb.PreviewKeyDown -= eventKeyPreview;
                tb.KeyDown -= eventKeyDown;
            };

            form.ShowDialog();
        }

        private static void eventKeyPreview(object sender, PreviewKeyDownEventArgs e)
        {
            //KEY PREVIEW
            Console.WriteLine("Key Preview: " + e.KeyData); //DETECT ENTER KEY
            //HOW CAN I CAPTURE HERE?
        }

        private static void eventKeyPress(object sender, KeyPressEventArgs e)
        {
            //KEY PRESS
            Console.WriteLine("Key Press: " + e.KeyChar); //DO NOT DETECT ENTER KEY
            e.Handled = true; //CAPTURE KEY PRESS
        }

        private static void eventKeyDown(object sender, KeyEventArgs e)
        {
            //KEY DOWN
            Console.WriteLine("Key Down: " + e.KeyData); //DO NOT DETECT ENTER KEY
            e.Handled = true; //CAPTURE KEY DOWN
        }
    }
}

Edit with the help from @GL SOFT INDIA:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Tools.Tester
{
    class Stackoverflow
    {
        public static void QuestionCaptureFunctionalKeys()
        {
            Form form = new Form();

            //CREATE TABLE
            DataGridView table = new CustomTable();
            table.Location = new Point(40, 40);
            table.Columns.Add("Column 1", "Column 1");
            table.Rows.Add();
            form.Controls.Add(table);

            //ADD EVENT LISTENER
            DataGridViewTextBoxEditingControl tb = null;
            table.EditingControlShowing += (s, e) =>
            {
                tb = e.Control as DataGridViewTextBoxEditingControl;
                tb.PreviewKeyDown += eventKeyPreview;           //CHECK OUT KEY PREVIEW
                tb.KeyPress += eventKeyPress;                   //CHECK OUT KEY PRESS
                tb.KeyDown += eventKeyDown;                     //CHECK OUT KEY DOWN
            };

            table.CellEndEdit += (s, e) =>
            {
                if (tb == null) return;
                tb.PreviewKeyDown -= eventKeyPreview;
                tb.KeyPress -= eventKeyPress;
                tb.KeyDown -= eventKeyDown;
            };

            form.ShowDialog();
        }

        class CustomTable : DataGridView
        {
            protected override bool ProcessDataGridViewKey(KeyEventArgs e)
            {
                //PROCESS DATAGRIDVIEW KEY - TRIGGER ONLY PREVIEW KEY IsInputKey = TRUE
                if (e.KeyCode == Keys.Enter)
                {
                    e.SuppressKeyPress = true;
                    e.Handled = true;
                    Console.WriteLine("ProcessDataGridViewKey: " + e.KeyData);
                }
                return base.ProcessDataGridViewKey(e);
            }
        }

        private static void eventKeyPreview(object sender, PreviewKeyDownEventArgs e)
        {
            //KEY PREVIEW
            Console.WriteLine("Key Preview: " + e.KeyData); //DETECT ENTER KEY
            e.IsInputKey = true; //<- MUST SET TRUE TO CAPTURE ProcessDataGridViewKey
        }

        private static void eventKeyPress(object sender, KeyPressEventArgs e)
        {
            //KEY PRESS
            Console.WriteLine("Key Press: " + e.KeyChar); //DO NOT DETECT ENTER KEY
            //e.Handled = true; //CAPTURE KEY PRESS
        }

        private static void eventKeyDown(object sender, KeyEventArgs e)
        {
            //KEY DOWN
            Console.WriteLine("Key Down: " + e.KeyData); //DO NOT DETECT ENTER KEY
            //e.Handled = true; //CAPTURE KEY DOWN
        }
    }
}

Result: the row selection still change on press enter. The e.SuppressKeyPress = true & e.Handled = true do not capture it. And important is the the PreviewKey Evnet IsImputKey settet to true(e.IsInputKey = true) to trigger the override event ProcessDataGridViewKey

enter image description here

Upvotes: 1

Views: 752

Answers (2)

Mar Tin
Mar Tin

Reputation: 2392

I found the solution in DataGridView TextBox cell Editing, Change Enter Key to Act Like Tab

Final code example:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Tools.Tester
{
    class Stackoverflow
    {
        public static void QuestionCaptureFunctionalKeys()
        {
            Form form = new Form();

            //CREATE TABLE
            DataGridView table = new CustomTable();
            table.Location = new Point(40, 40);
            table.Columns.Add("Column 1", "Column 1");
            table.Rows.Add();
            form.Controls.Add(table);

            //ADD EVENT LISTENER
            form.ShowDialog();
        }

        class CustomTable : DataGridView
        {
            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                if ((keyData == Keys.Enter) && (EditingControl != null))
                {
                    Console.WriteLine("ProcessCmdKey: " + keyData);
                    return true;
                }
                //for the rest of the keys, proceed as normal
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }
    }
}

Thanks to GL SOFT INDIA & Jason Brown

Upvotes: 0

GL SOFT INDIA
GL SOFT INDIA

Reputation: 64

you need to create custom datagridview class, then use AutoCompleteStringCollection in EditingControl to autofill options : -

private void dgvTable_EditingControlShowing(object sender, System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e) // Handles dgvTable.EditingControlShowing
{
    TextBox tbx = (TextBox)e.Control;
    string cn = dgvTable.Columns(dgvTable.CurrentCell.ColumnIndex).Name;
    if (cn == "Column1")
    {
        AutoCompleteStringCollection acs = new AutoCompleteStringCollection();

        foreach (DataRow dRow in dt.Rows)
            acs.Add(dRow("Column1").ToString);
        tbx.AutoCompleteCustomSource = acs;
        tbx.AutoCompleteSource = AutoCompleteSource.CustomSource;
        tbx.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    }
}

class myDgv : DataGridView
{
    protected override bool ProcessDataGridViewKey(System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            e.SuppressKeyPress = true;
            e.Handled = true;
        }
        else
            return base.ProcessDataGridViewKey(e);
    }
}

Upvotes: 1

Related Questions