最近一个朋友要做一个操作非常方便的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。如下图:


这篇文章给了我极大的启发,决定做一个给所有的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类的全部源代码:

ControlFocus
  1using System ;
  2using System.ComponentModel ;
  3using System.Collections ;
  4using System.Diagnostics ;
  5using System.Windows.Forms ;
  6using System.Drawing ;
  7using System.Drawing.Drawing2D ;
  8
  9namespace Shark
 10{
 11    
 12    [ProvideProperty( "NextControl", typeof(Component)) ]
 13    [ProvideProperty( "PreviousControl", typeof(Component)) ]
 14    public class ControlFocus:Component, IExtenderProvider
 15    {
 16        public ControlFocus()
 17        {
 18            //
 19            // TODO: Add constructor logic here
 20            //
 21        }
 22        Hashtable _hashTable = new Hashtable();
 23        Hashtable _previousHashTable = new Hashtable();
 24
 25        public Keys NextK;
 26        public Keys PreviousK;
 27
 28        public void SetNextControl(Component component,Control c)
 29        {
 30            if ( _hashTable.Contains( component ) != true)
 31            {
 32                //MessageBox.Show(component.ToString());
 33                _hashTable.Add(component,c);
 34                Control currentC = (Control)component;
 35                currentC.KeyDown +=new KeyEventHandler(currentC_KeyDown);
 36            }
 37            else
 38            {
 39                _hashTable[component] = c;
 40            }
 41        }
 42        public Control GetNextControl(Component component)
 43        {
 44            if( _hashTable.Contains( component ))
 45            {
 46                return (Control)_hashTable[ component ];
 47            }
 48            return null;
 49        }
 50
 51
 52        public void SetPreviousControl(Component component,Control c)
 53        {
 54            if ( _previousHashTable.Contains( component ) != true)
 55            {
 56                //MessageBox.Show(component.ToString());
 57                _previousHashTable.Add(component,c);
 58                Control currentC = (Control)component;
 59                currentC.KeyDown +=new KeyEventHandler(currentC_KeyDown);
 60            }
 61            else
 62            {
 63                _previousHashTable[component] = c;
 64            }
 65        }
 66        
 67        public Control GetPreviousControl(Component component)
 68        {
 69            if( _previousHashTable.Contains( component ))
 70            {
 71                return (Control)_previousHashTable[ component ];
 72            }
 73            return null;
 74        }
 75
 76        /**//// <summary>
 77        /// Used to retrieve the MenuImage extender property value
 78        /// for a given <c>MenuItem</c> component instance.
 79        /// </summary>
 80        /// <param name="component">the menu item instance associated with the value</param>
 81        /// <returns>Returns the MenuImage index property value for the specified <c>MenuItem</c> component instance.</returns>
 82        public string GetControlFocus( Component component )
 83        {
 84            if( _hashTable.Contains( component ))
 85                return (string) _hashTable[ component ] ;
 86
 87            return null;
 88        }
 89
 90        public ControlFocus(System.ComponentModel.IContainer container)
 91        {
 92            container.Add(this);
 93        }
 94        public bool CanExtend( object component )
 95        {
 96            // only support MenuItem objects that are not
 97            // top-level menus (default rendering for top-level
 98            // menus is fine - does not need extension
 99            if ( component is Control &&  !(component is Form))
100            {
101                return true;
102            }
103                
104            return false ;
105        }
106
107
108        private void currentC_KeyDown(object sender, KeyEventArgs e)
109        {
110            if(e.KeyCode == this.NextK)
111            {
112                Control nextControl = this.GetNextControl( (Component)sender);
113                if(nextControl != null && nextControl.CanFocus)
114                    nextControl.Focus();
115            }
116            else if(e.KeyCode == this.PreviousK)
117            {
118                Control previousControl = this.GetPreviousControl( (Component)sender);
119                if(previousControl != null && previousControl.CanFocus)
120                    previousControl.Focus();
121            }
122        }
123
124
125    }
126}
127

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

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

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

  1. linux同步某台服务器,linux 两台服务器之间开机自动同步指定目录下的文件

    linux 两台服务器之间开机自动同步指定目录下的文件 服务器B(172.17.166.11)上开机或者重启会自动拉取服务器A(172.17.166.10)上指定目录下所有文件 1.创建服务器B到服务 ...

  2. WPF中一个控件绑定另一个控件的属性

    原文:WPF中一个控件绑定另一个控件的属性 如同一个Grid中的一个按钮根据另一个按钮的显示与否作出不同的响应: 绑定的时候通过ElementName来指定控件 <Grid Margin=&qu ...

  3. C#,桌面编程入门(01)——按钮Button属性与事件、动态创建、快捷键、控件数组及自定义Button

    本文是<桌面编程入门>系列文章的开山之篇. <桌面编程入门>系列文章主要介绍桌面编程的各种组件,Button,ComboBox,Panel,WebBrowser...... 类 ...

  4. 超图js版 iclient 基本开发 - 加载基本图层(以天地图示例)和基本地图控件

    以超图的在线示例来学习:运行之后如下: 原网址代码比较长一些:先把多的去掉:看一下基本的加载图层和地图控件的概念: <script type="text/javascript" ...

  5. easyui combogrid分页加载默认值时无法自动跳转到相应页的解决方案

    2019独角兽企业重金招聘Python工程师标准>>> 当如果easyui combogrid分布加载时,有默认值,且默认值不在第一页时,此时combogrid里的显示值为idFie ...

  6. WinForm下ListBox控件“设置DataSource属性后无法修改项集合”的问题解决方案

    在做winform程序开发时,我用DataSource绑定了ListBox中的Item,但是当我进行一些添加.删除操作时就报了这个错"设置DataSource属性后无法修改项集合" ...

  7. Android相对布局(RelativeLayout)常用属性、练习使用按键、文本框等控件、线性布局(LinearLayout)属性

    RelativeLayout中子控件常用属性: 子控件默认是从父控件的左上角开始排列的 相对于父控件 android:layout_alignParentTop="true" 和父 ...

  8. wpf加载上千张图片部分图片不显示_开源WPF控件库MaterialDesignInXAML推荐

    (给DotNet加星标,提升.Net技能) 转自:沙漠之狐耶dotnet9.com/?p=2180 前言 介绍一个开源的C# WPF开源控件库,非常漂亮,重点是开源哦 WPF做桌面开发是很有优势的,除 ...

  9. Android View的elevation属性,CardView始终在布局顶层覆盖其它控件的解决方式;

    elevation属性字面意思是海拔.你可以理解为它是控件的厚度:我们日常使用控件都是只在乎宽高的,这个控件的厚度却很少在意:如果你不太理解,看一下CardView覆盖其它控件的例子: Android ...

最新文章

  1. ASP.NET2.0导航功能之配置会员和角色
  2. 【星球知识卡片】深度学习图像降噪有哪些关键技术点,如何学习
  3. 《 Docker 进阶与实战 》 读书笔记
  4. html页面内分栏显示不全,怎么消除Word文档分栏后栏间不平衡现象
  5. 计算机视觉、机器学习相关领域论文和源代码大集合--持续更新……(转载)
  6. 稳扎稳打 Silverlight 4.0 教程
  7. 洛谷1031 均分纸牌 解题报告
  8. static_cast vs reinterpret_cast vs const_cast vs dynamic_cast
  9. bat文件转exe工具分享
  10. 利用Bitvise SSH Client设置二级代理
  11. 智能家居APP的竞品分析报告(米家)
  12. 成功解决Cannot uninstall 'pywin32'. It is a distutils installed project and thus we cannot accurately de
  13. Ubuntu、Windows双系统下NTFS分区的读写问题
  14. 群晖虚拟机安装centos7.9
  15. Python基础入门之列表生成式
  16. P4语言的特性、P4语言和P4交换机的工作原理和流程简介
  17. 好用的IPv6归属地 API
  18. 新加坡环球影城:新加坡亲子游一日游好去处
  19. pdf如何安装最新版本的poppler?
  20. intel realsense 深度相机深度图像处理API总结

热门文章

  1. C语言小案例_小程序学习(三)
  2. 浙江大学副教授杨洋——《Time2Graph:从图视角出发的时间序列建模》
  3. 时序分析基本概念介绍ILM
  4. ilm 和dlm差异_NetPro 耐特普羅資訊:技術應用 / 漫談ILM與DLM-3 由DLM著手落實ILM的精神...
  5. 基于jsp,javaweb、ssm教务管理系统
  6. [附源码]Java计算机毕业设计SSM高铁乘坐舒适性在线调查及评价系统
  7. vue实现页面跳转过渡效果 transition
  8. 华农华迪实训训练-获得词频前10的字段数据-requests+Spark RDD
  9. matlab2c使用c++实现matlab函数系列教程-imag函数
  10. 产品logo的设计:图标与几何构成