给所有的Control添加发送键盘事件Tab事件,实现回车键自动跳转到下一个控件[基于Shark Xu]...

Shark Xu 的文章 给所有的Control加两个属性,实现回车键自动跳转到下一个控件 中给我们提供了一个方法实现的Windows应用程序中按回车键或者上下键,在输入项之间自动跳转。确实解决很大的问题,减少了代码量。 honyoung 提出利用发送Tab事件来解决这个问题。  即简化了设置两个属性的步骤(特别是在设置下一个或上一个时选择控件时很容易出错)。同时在VS.NET中又提供了TabIndex属性,这样很容易造成ControlFocus的属性和TabIndex不相匹配的地方。于是我在Shark Xu的基础上实现了“给所有的Control添加发送键盘事件Tab事件,实现回车键自动跳转到下一个控件 ”下面是具体的实现代码:
None.gif using  System;
None.gif
using  System.ComponentModel;
None.gif
using  System.Diagnostics;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Collections;
None.gif
None.gif
None.gif
namespace  sxu
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ProvideProperty(
"AllowKeyTab"typeof(Component))]
InBlock.gif    
public partial class KeyTab : Component, IExtenderProvider
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Keys NextK;
InBlock.gif        
public Keys PreviousK;
InBlock.gif        Hashtable _hashTable 
= new Hashtable();
InBlock.gif       
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Constructor#region Constructor
InBlock.gif        
public KeyTab()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public KeyTab(IContainer container)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            container.Add(
this);
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性AllowKeyTab#region 属性AllowKeyTab
InBlock.gif        
public void SetAllowKeyTab(Component component, bool  Allow)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Allow)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_hashTable.Contains(component) != true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//MessageBox.Show(component.ToString());
InBlock.gif
                    _hashTable.Add(component, Allow);
InBlock.gif                    Control currentC 
= (Control)component;
InBlock.gif                    currentC.KeyDown 
+= new KeyEventHandler(currentC_KeyDown);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (_hashTable.Contains(component) == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    _hashTable.Remove(component);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public bool  GetAllowKeyTab(Component component)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_hashTable.Contains(component))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用于属性检索
InBlock.gif          
/// </summary>
InBlock.gif        
/// <param name="component"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public bool GetKeyTab(Component component)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_hashTable.Contains(component))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (bool)_hashTable[component];
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void currentC_KeyDown(object sender, KeyEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
if (e.KeyCode == this.NextK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SendKeys.Send(
"{TAB}");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (e.KeyCode == this.PreviousK)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SendKeys.Send(
"+{TAB}");//发送shift+tab
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool CanExtend(object component)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//必须是普通控件(排出Form)才支持扩展 
InBlock.gif
            if (component is Control && !(component is Form))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif       
InBlock.gif       
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
在Form中的加入如下代码:
None.gif private   void  Form1_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
this.keyTab1.NextK = Keys.Down;
InBlock.gif            
this.keyTab1.PreviousK = Keys.Up;
ExpandedBlockEnd.gif        }
o_ddd.JPG
程序代码请 下载

转载于:https://www.cnblogs.com/ywolf123/archive/2006/08/25/486606.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值