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;
}
程序代码请
下载
查看全文
相关阅读:
Silverlight 操作Excel 中的进程资源释放问题
Silverlight DataGridTemplateColumn 中绑定事件
Silverlight 设置DataGrid中行的提示信息
判断Excel单元格中是否有错
读取配置文件生成简单的网站导航菜单
HTTP 错误 500.21 Internal Server Error
忽然有用到DataSet,标记学习一下
Silverlight读取包含中文的txt(解决乱码问题)
for/foreach/linq执行效率测试
将object强制转换成int效率测试
原文地址:https://www.cnblogs.com/ywolf123/p/486606.html
最新文章
jQuery json空对象筛选替换
对Web开发人员有用的8个网站
反转字符.递归算法
Javascript 匿名函数
Javascript倒计时源码.(时.分.秒)
一道15分的css题
精通JavaScript事件.详解
CSS Framework 960 Grid System (收)
IIS7 经典模式和集成模式的区别分析
22个HTML5的初级技巧
热门文章
rsync命令中的include参数顺序问题
12306快速输入验证码办法
Apache FTPServer安装为Windows服务的问题
Dell Inspiron One2330连接WPA2PSK的问题
adb shell后insufficient permissions for device的问题
Gentoo linux下Adobe AIR运行环境的安装
php升级到5.3后Discuz论坛不能使用的问题
gnome下使用xtile平铺窗口
Silverlight 操作Excel 中的进程资源释放问题(续)
疯狂的想法:自己弄个CMS
Copyright © 2011-2022 走看看