在DataGridView EditingControl中捕获EnterKey

时间:2019-04-15 06:58:16

标签: c# datagridview

我需要在DataGridView EditingControl中捕获EnterKey:

enter image description here

当我按下Enter时,只有PreviewKeyDown事件会检测到它。但是如何捕获InputKey来防止选择下一行?

最小示例:

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
        }
    }
}

在@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
        }
    }
}

结果:按Enter键后,行选择仍会更改。 The e.SuppressKeyPress = true & e.Handled = true不要捕获它。重要的是将PreviewKey Evnet IsImputKey settet设置为true(e.IsInputKey = true)来触发覆盖事件ProcessDataGridViewKey

enter image description here

2 个答案:

答案 0 :(得分:1)

您需要创建自定义datagridview类,然后在EditingControl中使用AutoCompleteStringCollection来自动填充选项:-

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);
    }
}

答案 1 :(得分:0)

我在DataGridView TextBox cell Editing, Change Enter Key to Act Like Tab

中找到了解决方案

最终代码示例:

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);
            }
        }
    }
}

感谢GL SOFT INDIA和Jason Brown