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;
}
程序代码请
下载
查看全文
相关阅读:
第十八课 顺序存储线性表的分析
第十七课 StaticList和DynamicList实现
第十六课 顺序存储结构的抽象实现
第十五课 线性表的顺序存储结构
第十四课 线性表的本质和操作
第十三课 类族结构的进化
第十二课 顶层父类的创建
第十一课 异常类构建
HDU 5773The All-purpose Zero
HDU 5755 Gambler Bo
原文地址:https://www.cnblogs.com/ywolf123/p/486606.html
最新文章
Luogu P4001 [ICPC-Beijing 2006]狼抓兔子
CF429E Points and Segments
BZOJ4883 [Lydsy1705月赛]棋盘上的守卫
Luogu P4042 [AHOI2014/JSOI2014]骑士游戏
BZOJ4423 [AMPPZ2013]Bytehattan
CF284E Coin Troubles
CF741C Arpa’s overnight party and Mehrdad’s silent entering
CF712E Memory and Casinos
grep的常用命令语法
ps&CPU
热门文章
UNIX和Linux信号
什么是云计算
数组队列C++实现
Linux内核2.6.14源码分析-双向循环链表代码分析(巨详细)
查看Linux各发行版本方法
nginx详解
Linux系统命令Top/free的使用及参数详解
网站压力测试工具集
第2课 C到C++的升级
第1课 学习C++的意义
Copyright © 2011-2022 走看看