[C#]Winform下回车或Tab键自动切换下一个控件焦点
满足用户体验,在数据录入时,能在输入完一个信息后通过回车或Tab键自动的切换到下一个控件(字段).
在界面控件设计时,默认可以通过设置控件的TabIndex来实现.但在布局调整时或者是对输入的内容有选择性时,从用代码的方式来处理显得更好维护一点.
完整的实现方法如下:
/// <summary>
/// 回车、Tab键盘切换或执行操作
/// </summary>
public sealed class TabEnter:IDisposable
{
private List<StringBuilder> ml;
private int i=0;
private System.Windows.Forms.Control mc;
/// <summary>
/// 知否启用Tab键功能
/// </summary>
private bool mallowTab=false;
/// <summary>
/// 是否启用Tab键切换/执行.
/// </summary>
public bool AllowTab
{
get { return mallowTab; }
set { mallowTab = value; }
}
public TabEnter(System.Windows.Forms.Control c)
{
ml = new List<StringBuilder>();
mc = c;
}
public TabEnter(System.Windows.Forms.Control c, bool allowTab):this(c)
{
mallowTab = allowTab;
}
public void Add(System.Windows.Forms.Control c)
{
c.KeyPress += KeyPressHandler;
c.TabIndex = i;
ml.Add(new StringBuilder(c.Name));
i += 1;
}
/// <summary>
/// 在需要独立处理KeyPress时间时,采用KeyUp来执行,当然可继续实现KeyDown
/// </summary>
/// <param name="c"></param>
public void AddKeyUp(System.Windows.Forms.Control c)
{
c.KeyUp += KeyUpHandler;
c.TabIndex = i;
ml.Add(new StringBuilder(c.Name));
i += 1;
}
private void KeyPressHandler(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((e.KeyChar == (Char)13) || (e.KeyChar == (Char)9 && mallowTab == true))
{
int j = ((System.Windows.Forms.Control)sender).TabIndex;
if (j >= ml.Count - 1) return;
string cname = ml[j + 1].ToString();
if (string.IsNullOrEmpty(cname)) return;
System.Windows.Forms.Control[] tca = mc.Controls.Find(cname, true);
if (tca == null || tca.Length == 0) return;
System.Windows.Forms.Control tc = tca[0];
if (tc == null) return;
System.Windows.Forms.Button b = tc as System.Windows.Forms.Button;
if (b != null)
b.PerformClick();
else
tc.Focus();
}
}
private void KeyUpHandler(Object sender, System.Windows.Forms.KeyEventArgs e)
{
if ((e.KeyCode == System.Windows.Forms.Keys.Enter) || (e.KeyCode == System.Windows.Forms.Keys.Tab && mallowTab == true))
{
int j = ((System.Windows.Forms.Control)sender).TabIndex;
if (j >= ml.Count - 1) return;
string cname = ml[j + 1].ToString();
if (string.IsNullOrEmpty(cname)) return;
System.Windows.Forms.Control[] tca = mc.Controls.Find(cname, true);
if (tca == null || tca.Length == 0) return;
System.Windows.Forms.Control tc = tca[0];
if (tc == null) return;
if (tc.GetType()==typeof(System.Windows.Forms.Button))
{
((System.Windows.Forms.Button)tc).PerformClick();
}
else
{
if (tc.Visible == true) tc.Focus();
} }
}
#region "资源释放"
public void Dispose()
{
Disposing(true);
GC.SuppressFinalize(this);
}
private bool m_disposed = false;
protected void Disposing(bool disposing)
{
if (!m_disposed)
{
if (disposing)
{
//Release managed resources
ml.Clear();
ml = null;
i = 0;
mc = null;
}
//Release unmanaged Resources
m_disposed = true;
}
} ~TabEnter()
{
Disposing(false);
}
#endregion
}
[C#]Winform下回车或Tab键自动切换下一个控件焦点的更多相关文章
- winform显示word、ppt和pdf,用一个控件显示
思路:都以pdf的格式展示,防止文件拷贝,所以要把word和ppt转换为pdf:展示用第三方组件O2S.Components.PDFView4NET.dll,破解版的下载链接:https://pan. ...
- 小技巧--tab键自动补齐Git命令
Git是什么,你不清楚? 好吧,那么该篇内容对你也木有帮助,请绕道而行.. 我们在使用Git命令时,可以通过tab键,自动补齐Git,特别是在切换分支时特别有用. 如下,当我们想将当前分支切换到bug ...
- CentOS6.5(2)----安装Tab键自动补全功能:bash-completion
首先要确保网络畅通,因为该过程要通过网络下载相关的软件包. 在 root 用户下,使用 cd ~/Downloads 命令进入下载文件夹,然后依次输入如下三个命令: [root@prime:~/Doc ...
- Python之Tab键自动补全
首先备份一下Tab键自动补全代码: # python start file import sys import readline import rlcompleter import atexit im ...
- python学习笔记--导入tab键自动补全功能的配置
今天开始学习Python,必须配置tab键补全功能 1.首先我们需要查看python的安装路径 [root@abc ~]# python Python 2.6.6 (r266:84292, Jan 2 ...
- python实现tab键自动补全
一.查询python安装路径,一般默认是/usr/lib64/ [root@host2 ~]# python Python (r266:, Jul , ::) [GCC (Red Hat -)] on ...
- Python中tab键自动补全功能的配置
新手学习Python的时候,如何没有tab键补全功能,我感觉那将是一个噩梦,对于我们这种菜鸟来说,刚接触python,对一切都不了解,还好有前辈们的指导,学习一下,并记录下来,还没有学习这个功能小伙伴 ...
- 修复duilib CEditUI控件和CWebBrowserUI控件中按Tab键无法切换焦点的bug
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41556615 在duilib中,按tab键会让焦点在Button一类的控 ...
- Winform开发中如何将数据库字段绑定到ComboBox控件
最近开始自己动手写一个财务分析软件,由于自己也是刚学.Net不久,所以自己写的的时候遇到了很多问题,希望通过博客把一些印象深刻的问题记录下来. Winform开发中如何将数据库字段绑定到ComboBo ...
随机推荐
- CentOS6.5安装图形界面
转载自http://www.cnblogs.com/zydev/p/5128788.html 一.使用网络安装(如果网络比较快,这个方法简单) yum groupinstall "Deskt ...
- 【转】shell脚本处理字符串的常用方法
转自:http://blog.csdn.net/linfeng999/article/details/6661233 1. 构造字符串 直接构造 STR_ZERO=hello #shell中等号左右的 ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- Codeforces Gym 100002 C "Cricket Field" 暴力
"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1000 ...
- 使用安卓读取sqlite数据库方法记录
最近要实现android读取sqlite数据库文件,在这里先做一个英汉字典的例子.主要是输入英语到数据库中查询相应的汉语意思,将其答案输出.数据库采用sqlite3. 如图: 实现过程完全是按照参考文 ...
- 模式识别 - 处理多演示样例学习(MIL)特征(matlab)
处理多演示样例学习(MIL)特征(matlab) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27206325 多演示样例学习( ...
- SparkGraphXTest.scala
/** * Created by root on 9/8/15. */ import org.apache.spark._ import org.apache.spark.graphx._ impor ...
- PHP.11-PHP实例(二)-面向对象实例(图形计算器)
面向对象实例(图形计算器) [PHP语法详解] 1.实现外观 #不同的动作,输出不同的表单 ###关于PHP中,无法使用localhost访问.php文件[http://www.360doc.com/ ...
- Java中 return 和finally
1. 最简单的情形 public void main(){ String s = test(); System.out.println("s=[" + s + "]&qu ...
- Lexia3 Citroen/Peugeot Diagnostic tool install instruction
We knew that Lexia-3 is a professional Citroen and Peugeot diagnostic interface, it’s both easy-usin ...