我想为表单上的所有可用控件运行一个通用的 KeyDown 偶数处理程序。 有什么办法可以实现这一点吗?
更清楚地说,我的目的是每当按下 Enter 键时检测它,并将焦点从当前控件移动到具有下一个 TabIndex 的控件。
我怎样才能做到这一点?
最佳答案
您必须避免妨碍正常使用 Enter 键。这应该很接近:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Enter && this.AcceptButton == null && this.ActiveControl != null) {
TextBoxBase box = this.ActiveControl as TextBoxBase;
if (box == null || !box.Multiline) {
// Not a dialog, not a multi-line textbox; we can use Enter for tabbing
this.SelectNextControl(this.ActiveControl, true, true, true, true);
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
关于.net - 有没有办法在 C#.NET 中使用 TabIndex 实现通用 KeyDown 处理程序和焦点移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2128562/