如何国米preT一个在C#回车键preSS为制表 [英] How to Interpret an Enter KeyPress as a Tab in C#
问题描述
我刚刚开始用C#开发的,而我工作的一个基于表单的项目,我想,当用户打开一个表格和$ P $执行一个标签行动pssed Enter键。
I just recently began in C# development, and I was working on a forms-based project and I am trying to perform a "tab" action when the user is on a form and pressed the Enter key.
我知道答案可能是很简单,但我在这个领域的新手。
I know the answer is probably quite simple, but I am a newbie in this realm.
推荐答案
欢迎到SO德州,
我相信有两种方法来实现这一目标,将只涉及添加:
I believe there are two methods to accomplish this, would just involve adding:
选项1:拼抢下一个控件,如果进行输入键preSS
在窗体的属性,设置在键preVIEW 的形式的财产,以真
。
In the properties of your form, set the KeyPreview property of the form to true
.
在code以下将捕获你的进入─preSS事件,并执行你正在寻找逻辑:
The code below will capture your "Enter-Press" event and perform the logic that you are looking for:
private void [YourFormName]_KeyDown(object sender, KeyEventArgs e)
{
Control nextControl ;
//Checks if the Enter Key was Pressed
if (e.KeyCode == Keys.Enter)
{
//If so, it gets the next control and applies the focus to it
nextControl = GetNextControl(ActiveControl, !e.Shift);
if (nextControl == null)
{
nextControl = GetNextControl(null, true);
}
nextControl.Focus();
//Finally - it suppresses the Enter Key
e.SuppressKeyPress = true;
}
}
这实际上允许用户以preSSShift + Enter转到程序选项卡也是如此。
This actually allows for the user to press "Shift+Enter" to go to the proceeding tab as well.
选项2:使用的SendKeys 方法
Option 2: Using the SendKeys method
private void [YourFormName]_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{TAB}");
}
}
我不知道,如果这种方法仍然是常用的,也可以被认为是一个黑客?我建议第一个,但我相信双方应该为你的需求。
I am not sure if this method is still commonly used or may be considered a "hack"? I would recommend the first one, but I believe both should work for your needs.
这篇关于如何国米preT一个在C#回车键preSS为制表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!