在winform中实现窗体上回车键代替TAB键
一、重写ProcessCmdKey方法
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
int WM_KEYDOWN = 256;
int WM_SYSKEYDOWN = 260;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg ==WM_SYSKEYDOWN))
{
switch(keyData)
{
case System.Windows.Forms.Keys.Enter:
SendKeys.Send("{Tab}");
return true;
}
{
int WM_KEYDOWN = 256;
int WM_SYSKEYDOWN = 260;
if ((msg.Msg == WM_KEYDOWN) || (msg.Msg ==WM_SYSKEYDOWN))
{
switch(keyData)
{
case System.Windows.Forms.Keys.Enter:
SendKeys.Send("{Tab}");
return true;
}
}
return base.ProcessCmdKey (ref msg, keyData);
}
二、对控件添加 KeyDown 事件,在事件方法中写如下代码
if (e.KeyData == Keys.Enter)
{
SendKeys.Send("{Tab}");
}
{
SendKeys.Send("{Tab}");
}
三、设置控件的 EnterMoveNextControl 属性为 True (只有Developer的可获取焦点的控件有此属性)