我需要在控制單元編輯中的單元格編輯期間接收按鍵事件。如何從DataGridView數據單元中獲取按下按鍵的按鍵事件
從我在網上發現的DataGridView
被設計爲將所有關鍵事件從DataGridView
傳遞到單元格編輯控件,並且您無法輕鬆獲取這些事件。
我發現這piece of code陷阱這些事件控制DataGrid
,但這並不適用於DataGridView
。
我需要在控制單元編輯中的單元格編輯期間接收按鍵事件。如何從DataGridView數據單元中獲取按下按鍵的按鍵事件
從我在網上發現的DataGridView
被設計爲將所有關鍵事件從DataGridView
傳遞到單元格編輯控件,並且您無法輕鬆獲取這些事件。
我發現這piece of code陷阱這些事件控制DataGrid
,但這並不適用於DataGridView
。
終於想通了。 這個難題有兩個部分 - 從單元格編輯控制中獲取密鑰並從DataGridView本身獲取密鑰。這是我的代碼。要使用它,您只需訂閱自定義事件:keyPressHook。
class KeyPressAwareDataGridView : DataGridView
{
protected override void OnControlAdded(ControlEventArgs e)
{
this.subscribeEvents(e.Control);
base.OnControlAdded(e);
}
protected override void OnControlRemoved(ControlEventArgs e)
{
this.unsubscribeEvents(e.Control);
base.OnControlRemoved(e);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
bool procesedInternally = false;
if (this.keyPressHook != null)
{
this.keyPressHook(this, e);
procesedInternally = e.SuppressKeyPress;
}
if (procesedInternally)
{
return true;
}
else
{
return base.ProcessDataGridViewKey(e);
}
}
private void subscribeEvents(Control control)
{
control.KeyDown += new KeyEventHandler(this.control_KeyDown);
control.ControlAdded += new ControlEventHandler(this.control_ControlAdded);
control.ControlRemoved += new ControlEventHandler(this.control_ControlRemoved);
foreach (Control innerControl in control.Controls)
{
this.subscribeEvents(innerControl);
}
}
private void unsubscribeEvents(Control control)
{
control.KeyDown -= new KeyEventHandler(this.control_KeyDown);
control.ControlAdded -= new ControlEventHandler(this.control_ControlAdded);
control.ControlRemoved -= new ControlEventHandler(this.control_ControlRemoved);
foreach (Control innerControl in control.Controls)
{
this.unsubscribeEvents(innerControl);
}
}
private void control_ControlAdded(object sender, ControlEventArgs e)
{
this.subscribeEvents(e.Control);
}
private void control_ControlRemoved(object sender, ControlEventArgs e)
{
this.unsubscribeEvents(e.Control);
}
private void control_KeyDown(object sender, KeyEventArgs e)
{
if (this.keyPressHook != null)
{
this.keyPressHook(this, e);
}
}
public event KeyEventHandler keyPressHook;
}
您必須重寫DataGridViewCell/DataGridViewTextBoxCell/otherTypes並處理派生類中的鍵*事件。
也許不如Mladen Prajdic上面的答案那麼好,但也許會更容易一些,具體取決於您的情況。您可以重寫表單或控件本身的ProcessCmdKey方法,處理那裏的按鍵,並檢查當前單元格。
試試這個:
class KeyPressAwareDataGridView : DataGridView
{
protected override void OnControlAdded(ControlEventArgs e)
{
SubscribeEvents(e.Control);
base.OnControlAdded(e);
}
protected override void OnControlRemoved(ControlEventArgs e)
{
UnsubscribeEvents(e.Control);
base.OnControlRemoved(e);
}
private void SubscribeEvents(Control control)
{
control.KeyPress += new KeyPressEventHandler(control_KeyPress);
control.ControlAdded += new ControlEventHandler(control_ControlAdded);
control.ControlRemoved += new ControlEventHandler(control_ControlRemoved);
foreach (Control innerControl in control.Controls)
{
SubscribeEvents(innerControl);
}
}
private void UnsubscribeEvents(Control control)
{
control.KeyPress -= new KeyPressEventHandler(control_KeyPress);
control.ControlAdded -= new ControlEventHandler(control_ControlAdded);
control.ControlRemoved -= new ControlEventHandler(control_ControlRemoved);
foreach (Control innerControl in control.Controls)
{
UnsubscribeEvents(innerControl);
}
}
private void control_ControlAdded(object sender, ControlEventArgs e)
{
SubscribeEvents(e.Control);
}
private void control_ControlRemoved(object sender, ControlEventArgs e)
{
UnsubscribeEvents(e.Control);
}
private void control_KeyPress(object sender, KeyPressEventArgs e)
{
// Apply your logic here whether this is the key pressed event you need.
// (e.g. "if(SelectedCells != null)")
MessageBox.Show(e.KeyChar.ToString());
}
}
我找到了部分解決方案通過聽EditingControlShowing並添加按鍵偵聽器,每一個新的編輯控制。
這使我可以訪問大多數的鍵,但我仍然沒有得到箭頭鍵。
我會試試Eren Aygunes的建議。
或者,對於那些不想爲這樣的occassions創建我們自己的DataGridView的用戶;有這種方法(在C++中):它使用DataGridView的EditingControlShowing事件。
private: System::Boolean fIsNonNumeric;
private: static System::Windows::Forms::KeyEventHandler^ EventKeyDown = nullptr;
private: static System::Windows::Forms::KeyPressEventHandler^ EventKeyPress = nullptr;
private: System::Void dataGridView_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
{
fIsNonNumeric= false;
// Determine whether the keystroke is a number from the top of the keyboard.
if (e->KeyCode < Keys::D0 || e->KeyCode > Keys::D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e->KeyCode < Keys::NumPad0 || e->KeyCode > Keys::NumPad9)
{
// Determine whether the keystroke is a backspace.
if (e->KeyCode != Keys::Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
fIsNonNumeric = true;
}
}
}
}
private: System::Void dataGridView_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
// Should we stop the character from being entered...?
if (fIsNonNumeric == true)
e->Handled = true;
}
private: System::Void dataGridView_Machines_EditingControlShowing(System::Object^ sender, System::Windows::Forms::DataGridViewEditingControlShowingEventArgs^ e)
{
if (nullptr == EventKeyDown)
EventKeyDown = (gcnew System::Windows::Forms::KeyEventHandler(this, &ProjectForm::dataGridView_KeyDown));
if (nullptr == EventKeyPress)
EventKeyPress = (gcnew System::Windows::Forms::KeyPressEventHandler(this, &ProjectForm::dataGridView_KeyPress));
e->Control->KeyDown -= EventKeyDown;
e->Control->KeyPress -= EventKeyPress;
e->Control->KeyDown += EventKeyDown;
e->Control->KeyPress += EventKeyPress;
}
您能否提供更多關於如何操作的信息? – eladyanai 2012-08-09 15:13:44