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;
}
程序代码请
下载
查看全文
相关阅读:
根据用户输入的时间查询那天的数据
动软 生成 linq相关DAO
pdf 移除密码 去除水印 批量去除水印 编辑文字 批量替换文字
利用OCR识别扫描的jpg、tif文件的文字
jstat命令详解
IDEA自动编译设置
IntelliJ IDEA:Field injection is not recommended
阿里巴巴Druid数据库连接池配置详解及使用
com.mysql.jdbc.Driver和com.mysql.cj.jdbc.Driver的区别
Java对元与分的金额的转换
原文地址:https://www.cnblogs.com/ywolf123/p/486606.html
最新文章
01——Introduction to Android介绍
Roller5.0.3安装配置部署 step by step
Android查询不到电话号码解决方法
jpush在有网的情况下6002
如何在TextView类中创建超链接 Linkify
oracle存储过程遇到的问题
【Oracle-PLsql】使用存储过程,利用table集合类型开发复杂业务报表
spring与quartz定时器
springmvc上传,下载
SpringMVC注解@RequestParam与RequestMapping全面解析
热门文章
js加载顺序
模板模式(部分方法延迟到子类实现)
maven 打包报错(增加调试信息)
Rest风格理解
@ResponseBody的作用
读万卷书
关于如何做计划(转自人人)
关于百度地图偏移的问题
ASP.NET 4.5 和 Visual Studio 2012 中的新功能
Windows搭建SVN
Copyright © 2011-2022 走看看