Мне нужно захватить EnterKey в DataGridView EditingControl
:
Когда я нажимаю 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
имел значение true (e.IsInputKey = true
), чтобы вызвать событие переопределения ProcessDataGridViewKey