录入界面回车快捷键的实现
需求:在winform开发中,登录界面或者其他的录入界面,一般用户都会有如下的快捷操作需求:在某个录入控件中按回车键,则界面的录入焦点自动转移到下一控件,焦点可以循环转移。
实现:
代码using System;
using System.Windows.Forms;
namespace LinkinTec.Core.WinForms
{
/// <summary>
/// 控件焦点控制类
/// </summary>
public class FocusByReturnHandler
{
public FocusByReturnHandler( Control control )
{
this.root = control;
this.SetControlsKeyEvent( root );
}
private Control root;
private void SetControlsKeyEvent( Control control )
{
if ( control.TabStop )
{
control.KeyDown +=new KeyEventHandler( control_KeyDown );
}
foreach( Control child in control.Controls )
{
this.SetControlsKeyEvent( child );
}
}
private void control_KeyDown(object sender, KeyEventArgs e)
{
if ( e.KeyCode.Equals( Keys.Return ))
{
SendKeys.SendWait( "{TAB}" );
}
}
}
}
using System.Windows.Forms;
namespace LinkinTec.Core.WinForms
{
/// <summary>
/// 控件焦点控制类
/// </summary>
public class FocusByReturnHandler
{
public FocusByReturnHandler( Control control )
{
this.root = control;
this.SetControlsKeyEvent( root );
}
private Control root;
private void SetControlsKeyEvent( Control control )
{
if ( control.TabStop )
{
control.KeyDown +=new KeyEventHandler( control_KeyDown );
}
foreach( Control child in control.Controls )
{
this.SetControlsKeyEvent( child );
}
}
private void control_KeyDown(object sender, KeyEventArgs e)
{
if ( e.KeyCode.Equals( Keys.Return ))
{
SendKeys.SendWait( "{TAB}" );
}
}
}
}
使用:在Form中声明一个变量,private FocusByReturnHandler fh; 在构造方法中创建对象, this.fh= new FocusByReturnHandler(this);

浙公网安备 33010602011771号