zoukankan      html  css  js  c++  java
  • 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;
    }
    }

    }



    返回导读目录,阅读更多随笔



    分割线,以下为博客签名:

    软件臭虫情未了
    • 编码一分钟
    • 测试十年功


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

  • 相关阅读:
    GPT(4kb硬盘) 单硬盘装变色龙、GAH61MAD2V、ALC887VD、HD6570成功驱动经验(转)
    unable to dequeue a cell with identifier Cell must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
    2013.5.29
    平等博弈
    组合数学
    哈密顿+欧拉图
    差分约束
    11.11
    如何直接跳出多重循环
    摘要:数组练习与部分字符串练习
  • 原文地址:https://www.cnblogs.com/08shiyan/p/1814789.html
Copyright © 2011-2022 走看看