zoukankan
html css js c++ java
给所有的Control添加发送键盘事件Tab事件,实现回车键自动跳转到下一个控件[基于Shark Xu]
在
Shark Xu
的文章
给所有的Control加两个属性,实现回车键自动跳转到下一个控件
中给我们提供了一个方法实现的Windows应用程序中按回车键或者上下键,在输入项之间自动跳转。确实解决很大的问题,减少了代码量。
honyoung
提出利用发送Tab事件来解决这个问题。 即简化了设置两个属性的步骤(特别是在设置下一个或上一个时选择控件时很容易出错)。同时在VS.NET中又提供了TabIndex属性,这样很容易造成ControlFocus的属性和TabIndex不相匹配的地方。于是我在Shark Xu的基础上实现了“给所有的Control添加发送键盘事件Tab事件,实现回车键自动跳转到下一个控件 ”下面是具体的实现代码:
using
System;
using
System.ComponentModel;
using
System.Diagnostics;
using
System.Text;
using
System.Windows.Forms;
using
System.Collections;
namespace
sxu
{
[ProvideProperty(
"
AllowKeyTab
"
,
typeof
(Component))]
public
partial
class
KeyTab : Component, IExtenderProvider
{
public
Keys NextK;
public
Keys PreviousK;
Hashtable _hashTable
=
new
Hashtable();
Constructor
#region
Constructor
public
KeyTab()
{
InitializeComponent();
}
public
KeyTab(IContainer container)
{
container.Add(
this
);
InitializeComponent();
}
#endregion
属性AllowKeyTab
#region
属性AllowKeyTab
public
void
SetAllowKeyTab(Component component,
bool
Allow)
{
if
(Allow)
{
if
(_hashTable.Contains(component)
!=
true
)
{
//
MessageBox.Show(component.ToString());
_hashTable.Add(component, Allow);
Control currentC
=
(Control)component;
currentC.KeyDown
+=
new
KeyEventHandler(currentC_KeyDown);
}
}
else
{
if
(_hashTable.Contains(component)
==
true
)
{
_hashTable.Remove(component);
}
}
}
public
bool
GetAllowKeyTab(Component component)
{
if
(_hashTable.Contains(component))
{
return
true
;
}
return
false
;
}
#endregion
/**/
///
<summary>
///
用于属性检索
///
</summary>
///
<param name="component"></param>
///
<returns></returns>
public
bool
GetKeyTab(Component component)
{
if
(_hashTable.Contains(component))
{
return
(
bool
)_hashTable[component];
}
return
false
;
}
private
void
currentC_KeyDown(
object
sender, KeyEventArgs e)
{
if
(e.KeyCode
==
this
.NextK)
{
SendKeys.Send(
"
{TAB}
"
);
}
else
if
(e.KeyCode
==
this
.PreviousK)
{
SendKeys.Send(
"
+{TAB}
"
);
//
发送shift+tab
}
}
public
bool
CanExtend(
object
component)
{
//
必须是普通控件(排出Form)才支持扩展
if
(component
is
Control
&&
!
(component
is
Form))
{
return
true
;
}
return
false
;
}
}
}
在Form中的加入如下代码:
private
void
Form1_Load(
object
sender, EventArgs e)
{
this
.keyTab1.NextK
=
Keys.Down;
this
.keyTab1.PreviousK
=
Keys.Up;
}
程序代码请
下载
查看全文
相关阅读:
js:值类型/引用类型/内存回收/函数传值
JS学习计划
起点
哈夫曼压缩/解压缩(控制台,C++)
二维数组作为函数参数传递(C++)
二级指针和指针引用函数传参(C++)
学生管理系统(C++,控制台,文件读取,姓名排序)
C++的getline()和get()函数
二叉排序树节点的删除(C++,算法导论),前中后序遍历(递归/非递归,栈实现),按层次遍历(队列实现)
QT程序打包成EXE
原文地址:https://www.cnblogs.com/ywolf123/p/486606.html
最新文章
Excel如何制作 联想动态下拉菜单
excel 隐藏数据的方式
转载:https://blog.csdn.net/qq_22706515/article/details/52595027
java语言导学(5版)--第12章并发之二
转载:java基础之单例
springboot学习网站及博客
springboot学习入门之五---开发Web应用之JSP篇
转载:http://www.cnblogs.com/double-K/p/6926367.html
web前端框架
springboot学习入门之四---开发Web应用之Thymeleaf篇
热门文章
web服务器
java书籍
linux系统运行时间
IBM AIX系统硬件配置信息 查看命令----lscfg
js:原型/原型链,first chasing
问题记录:sass/Emment配置中出现的问题
js:闭包续
js:如何文艺地理解闭包
Canvas 2D小游戏开发总结-1
js:匿名函数/闭包前奏
Copyright © 2011-2022 走看看