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;
}
程序代码请
下载
查看全文
相关阅读:
Windows Azure Cloud Service (5) 由过渡环境向生产环境过渡
rpcss.dll丢失造成任务栏不见
css文本省略号
字符串是否包含中文?
在 System.NullReferenceException 中第一次偶然出现的“ComServer.exe”类型的异常
取参数的正则表达式
EverNote死机的问题
找尺子
读书笔记
水晶按钮的学习
原文地址:https://www.cnblogs.com/ywolf123/p/486606.html
最新文章
android 动画
android 两坐标画直线
Adapter 数据缓存
android 安装
数据库读图片
nginx 服务器
java 随机数
android noTouch (二)
android noTouch 事件
错误 No enclosing instance of type WallpaperService is available due to some intermediate constructor invocation
热门文章
网站
Windows Azure Cloud Service (24) 使用Startup注册COM组件(上)
Windows Azure Cloud Service (23) 使用Full IIS模式部署多站点和虚拟目录
Windows Azure Cloud Service (9) Configuration的变更和通知机制
Windows Azure Cloud Service (21) 通过远程桌面功能访问Windows Azure 计算节点
SQL Azure(十一) SQL Azure Data Sync数据同步功能(下)
Windows Azure Cloud Service (20) 使用Internal Endpoint实现Role的内部通信
Windows Azure Cloud Service (22) Web Role的Full IIS特性
Windows Azure Cloud Service (8) Role及其生命周期模型
SQL Azure(十) SQL Azure Data Sync数据同步功能(上)
Copyright © 2011-2022 走看看