Hi experts,
in an application I am developing there is a
DataGridView
. For reasons out of the scope of this question user shall not be allowed to edit the grid's content directly.
Instead, on beginning cell edit, the operation is immediately cancelled. A textbox is being displayed right where the original cell would have been.
MyDataGridView.cs:
protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
{
e.Cancel = true;
MyTextBox textBox = new MyTextBox(
e.ColumnIndex, e.RowIndex,
this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()
);
System.Drawing.Rectangle cellRectangle = this.GetCellDisplayRectangle(
e.ColumnIndex, e.RowIndex,
false
);
textBox.Location = cellRectangle.Location;
textBox.Size = cellRectangle.Size;
textBox.Finished += new EventHandler(textBox_Finished);
this.Controls.Add(textBox);
textBox.Focus();
}
User can then edit int the special text box.
MyTextBox.cs:
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == System.Windows.Forms.Keys.Escape)
{
this.Text = _initialText;
if (Finished != null)
Finished(this, new EventArgs());
}
else if (e.KeyCode == System.Windows.Forms.Keys.Enter)
{
if (Finished != null)
Finished(this, new EventArgs());
}
}
The
Finished
event handler then transfers the text box value to the grid and destroys the text box.
My problem at the moment is that above
OnKeyDown
event doesn't fire on Enter nor on zero
nor on arrow keys. Other keys seem to work as expected.
How can I get all keys to work?