C#中方向键与回车键切换控件焦点的两种方法


本文摘自PHP中文网,作者零下一度,侵删。

环境:界面上有TextBox,ComboBox等控件。

不建议把左右方向键都用来切换焦点,否则你在TextBox里面改变光标所在字符位置就不方便了。

方法一:笨方法,需为每个控件单独注册事件处理

以TextBox为例,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

1 private void textbox_KeyDown(object sender, KeyEventArgs e)        

 2 {            

 3     if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Enter)            

 4     {                

 5         e.SuppressKeyPress = true;                

 6         System.Windows.Forms.SendKeys.Send("{Tab}");            

 7     }            

 8     else if (e.KeyCode == Keys.Up)            

 9     {                

10         e.SuppressKeyPress = true;                

11         System.Windows.Forms.SendKeys.Send("+{Tab}");            

12     }        

13 }

方法二:简单方法,无需为每个控件单独注册事件处理,仅需在窗体类上加入如下代码:

1

2

3

4

5

6

7

8

9

10

1 //上、下方向键,及回车键切换控件焦点 2 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 3 { 4     Keys key = (keyData & Keys.KeyCode); 5     if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Enter)            

 6     {                                 

 7       System.Windows.Forms.SendKeys.Send("{Tab}");

 8       return true;           

 9     }            

10     else if (e.KeyCode == Keys.Up)            

11     {                                 

12       System.Windows.Forms.SendKeys.Send("+{Tab}");13       return true;            

14     }  

15     return base.ProcessCmdKey(ref msg, keyData);16 }

到此,切换控件焦点的功能已实现,现在有个新的需求,窗体界面上有两个ComboBox控件cmbMeas和cmbRemark,我希望在这两个控件上Enter回车时提交,而不是切换焦点,那怎么办呢?那就需要判断当前拥有焦点的控件是不是cmbMeas或cmbRemark,上面的代码需要稍微改动下,具体代码如下:

1

2

3

4

5

6

7

8

9

10

1 //API声明:获取当前焦点控件句柄       2 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)] 3 internal static extern IntPtr GetFocus(); 4  5 //获取当前拥有焦点的控件 6 private Control GetFocusedControl() 7 { 8      Control focusedControl = null; 9      // To get hold of the focused control:10      IntPtr focusedHandle = GetFocus();11      if (focusedHandle != IntPtr.Zero)12          //focusedControl = Control.FromHandle(focusedHandle);13          focusedControl = Control.FromChildHandle(focusedHandle);14      return focusedControl ;15  }16 17 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)18 {19     Keys key = (keyData & Keys.KeyCode);20     Control ctrl = GetFocusedControl();21     if (e.KeyCode == Keys.Down || (key == Keys.Enter && ctrl.Name != "cmbMeas" && ctrl.Name != "cmbRemark"))            

22     {                                 

23         System.Windows.Forms.SendKeys.Send("{Tab}");

24         return true;           

25     }            

26     else if (e.KeyCode == Keys.Up)            

27     {                                 

28         System.Windows.Forms.SendKeys.Send("+{Tab}");29         return true;            

30     }  

31     return base.ProcessCmdKey(ref msg, keyData);32 }

说明:

Control.FromHandle 方法

阅读剩余部分

相关阅读 >>

.net页面局部更新引发的思考

c#中ini配置文件的图文代码详解

.net中xml转换成treeview视图

.net core中如何使用entity framework操作postgresql?

代码分析:在.net core中使用ref和span<t>提高程序性能

asp.net c#中application的用法教程

jquery pqgrid分页控件乱码是什么情况?

ants performance profiler(.net性能调优教程)

关于.net c# sql数据库sqlhelper类实例代码

关于c#如何实现access以时间段查询出来的的数据添加到listview中

更多相关阅读请进入《csharp》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...