当前位置: 编程技术>.net/c#/asp.net
WinForm中变Enter键为Tab键实现焦点转移的方法
来源: 互联网 发布时间:2014-11-03
本文导语: 本文实例讲述了WinForm中变Enter键为Tab键实现焦点转移的方法,在进行C#应用程序开发时有一定的实用价值。分享给大家供大家参考。 具体实现代码如下: /// /// 窗体控件控制相关的方法 /// public class ControlTools { private ...
本文实例讲述了WinForm中变Enter键为Tab键实现焦点转移的方法,在进行C#应用程序开发时有一定的实用价值。分享给大家供大家参考。
具体实现代码如下:
///
/// 窗体控件控制相关的方法
///
public class ControlTools
{
private Form frm;
public ControlTools(Form frm)
{
this.frm = frm;
}
///
/// 窗体上所有子控件的回车设成Tab
///
public void EnterToTab()
{
frm.KeyPreview = true;
frm.KeyPress += new KeyPressEventHandler(frm_KeyPress);
}
///
/// 注册窗体的KeyPress事件
///
///
///
private void frm_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
frm.SelectNextControl(frm.ActiveControl, true, true, true, true);
}
}
///
/// 把某一个控件的所有子控件(TextBox ComboBox)的回车设成Tab
///
/// 容器控件
public void EnterToTab(Control groupControl)
{
foreach (Control control in groupControl.Controls)
{
if (control is TextBox || control is ComboBox)
control.KeyPress += new KeyPressEventHandler(control_KeyPress);
}
}
///
/// 注册控件的KeyPress事件
///
///
///
private void control_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
SendKeys.Send("{Tab}");
e.Handled = false;
}
}
}
希望本文所述变Enter键为Tab键的方法对大家C#程序设计有所帮助。