分享

WinForm中 变Enter键为Tab键 实现焦点转移

 唐伯龙 2011-08-03

WinForm中 变Enter键为Tab键 实现焦点转移

代码不是我的哦,嘿嘿……
/// <summary>
/// 窗体控件控制相关的方法
/// </summary>
public class ControlTools
{
private Form frm;

public ControlTools(Form frm)
{
this.frm = frm;
}

/// <summary>
/// 窗体上所有子控件的回车设成Tab
/// </summary>
public void EnterToTab()
{
frm.KeyPreview
= true;

frm.KeyPress
+= new KeyPressEventHandler(frm_KeyPress);
}
/// <summary>
/// 注册窗体的KeyPress事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frm_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
frm.SelectNextControl(frm.ActiveControl,
true, true, true, true);
}
}

/// <summary>
/// 把某一个控件的所有子控件(TextBox ComboBox)的回车设成Tab
/// </summary>
/// <param name="groupControl">容器控件</param>
public void EnterToTab(Control groupControl)
{
foreach (Control control in groupControl.Controls)
{
if (control is TextBox || control is ComboBox)
control.KeyPress
+= new KeyPressEventHandler(control_KeyPress);
}
}

/// <summary>
/// 注册控件的KeyPress事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void control_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
SendKeys.Send(
"{Tab}");
e.Handled
= false;
}
}

}

 

变Enter为Tab
/// <summary>
/// 窗体控件控制相关的方法
/// </summary>
public class ControlTools
{
private Form frm;

public ControlTools(Form frm)
{
this.frm = frm;
}

/// <summary>
/// 窗体上所有子控件的回车设成Tab
/// </summary>
public void EnterToTab()
{
frm.KeyPreview
= true;

frm.KeyPress
+= new KeyPressEventHandler(frm_KeyPress);
}
/// <summary>
/// 注册窗体的KeyPress事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frm_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
frm.SelectNextControl(frm.ActiveControl,
true, true, true, true);
}
}

/// <summary>
/// 把某一个控件的所有子控件(TextBox ComboBox)的回车设成Tab
/// </summary>
/// <param name="groupControl">容器控件</param>
public void EnterToTab(Control groupControl)
{
foreach (Control control in groupControl.Controls)
{
if (control is TextBox || control is ComboBox)
control.KeyPress
+= new KeyPressEventHandler(control_KeyPress);
}
}

/// <summary>
/// 注册控件的KeyPress事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void control_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
SendKeys.Send(
"{Tab}");
e.Handled
= false;
}
}

}

 

 





随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

个性签名:
信心、恒心、决心和耐心,应该属于世界上的每一个人,同样也属于我自己。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约