winform获取当前控件焦点,控件名找到具体控件,键盘上下键绑定事件

1.获取控件焦点

 [DllImport("user32.dll")]
        public static extern IntPtr GetFocus();
        private Control GetFocusedControl()
        {
            IntPtr handle = GetFocus();
            Control focusedControl = Control.FromHandle(handle);
            return focusedControl;
        }

2.通过控件名找到具体控件

   private Control getControlFromName(Control container, string controlName)
        {

            foreach (Control c in container.Controls)
            {
                if (c.Name == controlName)
                {
                    return c;
                }
                if (c.HasChildren)
                {
                    //这里不能直接写return  getControlFromName(c, controlName);因为会导致只执行第一个子容器控件,如果未找到就提前返回了Null;
                    Control myControl = getControlFromName(c, controlName);
                    if (myControl != null)
                    {
                        return myControl;
                    }
                }
            }
            //并非所有的代码路径都返回值,未找到就返回null
            return null;
        }

3.具体调用

 private void F_AQL_BA_Entry_KeyDown(object sender, KeyEventArgs e)
        {
            string focusedControl = GetFocusedControl().Name;


            // 键码获取
            if ((Keys)e.KeyCode == Keys.Left)
            {
                Debug.WriteLine("左键");
                int tbnum = int.Parse(focusedControl.Split('Q')[1]) - 1;
                if (tbnum > 0)
                {
                    string tbX = "tb" + tbnum;
                    TextBox control = getControlFromName(this, tbX) as TextBox;
                    control.Focus();
                }
               
            }
            else if ((Keys)e.KeyCode == Keys.Right)
            {
                int tbnum = int.Parse(focusedControl.Split('Q')[1]) + 1;
                if (tbnum < 10)
                {
                    string tbX = "tbQ" + tbnum;
                    //TextBox c = this.Controls["tb2"] as TextBox;
                    TextBox control = getControlFromName(this, tbX) as TextBox;
                    control.Focus();
                }
               
            }
            else if ((Keys)e.KeyCode == Keys.Up)
            {
                int tbnum = int.Parse(focusedControl.Split('Q')[1]) + 1;
                Debug.WriteLine("上键");
                if (tbnum > 9&&tbnum<= 12)
                {
                    string tbX = "tbQ" + tbnum;
                    //TextBox c = this.Controls["tb2"] as TextBox;
                    TextBox control = getControlFromName(this, tbX) as TextBox;
                    control.Focus();
                }
                else
                {
                    TextBox control = getControlFromName(this, "tbQ10") as TextBox;
                    control.Focus();
                }

            }
            else if ((Keys)e.KeyCode == Keys.Down)
            {
                int tbnum = int.Parse(focusedControl.Split('Q')[1]) - 1;
                Debug.WriteLine("下键");
                if (tbnum > 9)
                {
                    string tbX = "tbQ" + tbnum;
                    //TextBox c = this.Controls["tb2"] as TextBox;
                    TextBox control = getControlFromName(this, tbX) as TextBox;
                    control.Focus();
                }
                else
                {
                    TextBox control = getControlFromName(this, "tbQ1") as TextBox;
                    control.Focus();
                }
            }
        }

4.重点:要把窗体的KeyPriview设置属性为true,键盘绑定事件才会生效

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值