给所有的Control加两个属性,实现回车键自动跳转到下一个控件

最近一个朋友要做一个操作非常方便的Windows应用程序,就是希望通过按回车键或者上下键,在输入项之间自动跳转,国人都不习惯
使用Tab/Shift Tab在输入项之间的跳转。我之前也使用了一些别的方法,如:首先将需要跳转的输入项放置到一个集合对象ArrayList里;
然后设置Form的PreKeyView为True;最后 给Form添加一个KeyDown事件处理程序。虽然是一个办法。但总觉得不够专业。
前不久在WWW.CodeProject.COM看了一个老外写的一篇文章http://www.codeproject.com/cs/menu/menuimage.asp,是关于如何做一个带
图标的菜单的,大部分人写这样的程序都是重写MenuItem,重载MenuItme的OnDrawItem和OnMeasureItme方法,可是这位老大独树一帜,
去做了一个叫MenuImage的类,它实现了System.ComponentModel.IExtenderProvider 接口,在设计期给MenuItem扩展了一个属性
MenuImage。如下图:

o_MenuImage.GIF
这篇文章给了我极大的启发,决定做一个给所有的Control“扩展” 两个属性NextControl和PreviousControl,这样按回车或者上下键就可以跳转了。
首先创建一个类ControlFocus
[ProvideProperty( "NextControl", typeof(Component)) ]
[ProvideProperty( "PreviousControl", typeof(Component)) ]
public class ControlFocus:Component, IExtenderProvider
第一行和第二行是什么意思呢,就是这个ExtenderProvider会给别的Control扩展两个属性NextControl和PreviousControl
同时要求在ControlFocus类里面包含下面四个方法:
GetNextControl/SetNextControl/GetPreviousControl/SetPreviousControl
另外ControlFocus这个ExtenderProvider必须实现IExtenderProvider的CanExtend方法。
下面就是ControlFocus类的全部源代码:

ContractedBlock.gif ExpandedBlockStart.gif ControlFocus
  1None.gifusing System ;
  2None.gifusing System.ComponentModel ;
  3None.gifusing System.Collections ;
  4None.gifusing System.Diagnostics ;
  5None.gifusing System.Windows.Forms ;
  6None.gifusing System.Drawing ;
  7None.gifusing System.Drawing.Drawing2D ;
  8None.gif
  9None.gifnamespace Shark
 10ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 11InBlock.gif    
 12InBlock.gif    [ProvideProperty( "NextControl"typeof(Component)) ]
 13InBlock.gif    [ProvideProperty( "PreviousControl"typeof(Component)) ]
 14InBlock.gif    public class ControlFocus:Component, IExtenderProvider
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif        public ControlFocus()
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 18InBlock.gif            //
 19InBlock.gif            // TODO: Add constructor logic here
 20InBlock.gif            //
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif        Hashtable _hashTable = new Hashtable();
 23InBlock.gif        Hashtable _previousHashTable = new Hashtable();
 24InBlock.gif
 25InBlock.gif        public Keys NextK;
 26InBlock.gif        public Keys PreviousK;
 27InBlock.gif
 28InBlock.gif        public void SetNextControl(Component component,Control c)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 30InBlock.gif            if ( _hashTable.Contains( component ) != true)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 32InBlock.gif                //MessageBox.Show(component.ToString());
 33InBlock.gif                _hashTable.Add(component,c);
 34InBlock.gif                Control currentC = (Control)component;
 35InBlock.gif                currentC.KeyDown +=new KeyEventHandler(currentC_KeyDown);
 36ExpandedSubBlockEnd.gif            }

 37InBlock.gif            else
 38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 39InBlock.gif                _hashTable[component] = c;
 40ExpandedSubBlockEnd.gif            }

 41ExpandedSubBlockEnd.gif        }

 42InBlock.gif        public Control GetNextControl(Component component)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            if( _hashTable.Contains( component ))
 45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 46InBlock.gif                return (Control)_hashTable[ component ];
 47ExpandedSubBlockEnd.gif            }

 48InBlock.gif            return null;
 49ExpandedSubBlockEnd.gif        }

 50InBlock.gif
 51InBlock.gif
 52InBlock.gif        public void SetPreviousControl(Component component,Control c)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 54InBlock.gif            if ( _previousHashTable.Contains( component ) != true)
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 56InBlock.gif                //MessageBox.Show(component.ToString());
 57InBlock.gif                _previousHashTable.Add(component,c);
 58InBlock.gif                Control currentC = (Control)component;
 59InBlock.gif                currentC.KeyDown +=new KeyEventHandler(currentC_KeyDown);
 60ExpandedSubBlockEnd.gif            }

 61InBlock.gif            else
 62ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 63InBlock.gif                _previousHashTable[component] = c;
 64ExpandedSubBlockEnd.gif            }

 65ExpandedSubBlockEnd.gif        }

 66InBlock.gif        
 67InBlock.gif        public Control GetPreviousControl(Component component)
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 69InBlock.gif            if( _previousHashTable.Contains( component ))
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 71InBlock.gif                return (Control)_previousHashTable[ component ];
 72ExpandedSubBlockEnd.gif            }

 73InBlock.gif            return null;
 74ExpandedSubBlockEnd.gif        }

 75InBlock.gif
 76ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 77InBlock.gif        /// Used to retrieve the MenuImage extender property value
 78InBlock.gif        /// for a given <c>MenuItem</c> component instance.
 79InBlock.gif        /// </summary>
 80InBlock.gif        /// <param name="component">the menu item instance associated with the value</param>
 81ExpandedSubBlockEnd.gif        /// <returns>Returns the MenuImage index property value for the specified <c>MenuItem</c> component instance.</returns>

 82InBlock.gif        public string GetControlFocus( Component component )
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 84InBlock.gif            if( _hashTable.Contains( component ))
 85InBlock.gif                return (string) _hashTable[ component ] ;
 86InBlock.gif
 87InBlock.gif            return null;
 88ExpandedSubBlockEnd.gif        }

 89InBlock.gif
 90InBlock.gif        public ControlFocus(System.ComponentModel.IContainer container)
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 92InBlock.gif            container.Add(this);
 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif        public bool CanExtend( object component )
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            // only support MenuItem objects that are not
 97InBlock.gif            // top-level menus (default rendering for top-level
 98InBlock.gif            // menus is fine - does not need extension
 99InBlock.gif            if ( component is Control &&  !(component is Form))
100ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
101InBlock.gif                return true;
102ExpandedSubBlockEnd.gif            }

103InBlock.gif                
104InBlock.gif            return false ;
105ExpandedSubBlockEnd.gif        }

106InBlock.gif
107InBlock.gif
108InBlock.gif        private void currentC_KeyDown(object sender, KeyEventArgs e)
109ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
110InBlock.gif            if(e.KeyCode == this.NextK)
111ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
112InBlock.gif                Control nextControl = this.GetNextControl( (Component)sender);
113InBlock.gif                if(nextControl != null && nextControl.CanFocus)
114InBlock.gif                    nextControl.Focus();
115ExpandedSubBlockEnd.gif            }

116InBlock.gif            else if(e.KeyCode == this.PreviousK)
117ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
118InBlock.gif                Control previousControl = this.GetPreviousControl( (Component)sender);
119InBlock.gif                if(previousControl != null && previousControl.CanFocus)
120InBlock.gif                    previousControl.Focus();
121ExpandedSubBlockEnd.gif            }

122ExpandedSubBlockEnd.gif        }

123InBlock.gif
124InBlock.gif
125ExpandedSubBlockEnd.gif    }

126ExpandedBlockEnd.gif}

127None.gif


其次创建一个示例窗口.如下:

o_ControlFocus.GIF

程序的全部源代码,请下载:

 

 

 


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值