这个问题在这里向我展示了解决它的方法。这是代码:
class MyDataGridView : DataGridView
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData == Keys.Enter) && (this.EditingControl != null))
{
//new behaviour for Enter
TextBox tb = (TextBox)EditingControl;
int pos = tb.SelectionStart;
tb.Text = tb.Text.Remove(pos, tb.SelectionLength);
tb.Text = tb.Text.Insert(pos, Environment.NewLine);
tb.SelectionStart = pos + Environment.NewLine.Length;
tb.ScrollToCaret();
//and do nothing else
return true;
}
else if ((keyData == Keys.Up) && (this.EditingControl != null))
{
//programmatically move caret up
//(look at related question to see how)
return true;
}
else if ((keyData == Keys.Down) && (this.EditingControl != null))
{
//programmatically move caret down
//(look at related question to see how)
return true;
}
//for the rest of the keys, proceed as normal
return base.ProcessCmdKey(ref msg, keyData);
}
}
所以这是对 DataGridView 的简单更改,它可以工作。我只需要
- 创建这个新类,并且
- 将 DesignerClass 中的两行更改为使用 MyDataGridView 而不是 DataGridView(声明和初始化)
其他一切都按预期工作。
相关问题:如何以编程方式将插入符号上下移动一行。