如何在C#中禁用{TAB}和{SUBTRACT}键盘?

问题描述:

我有一个表单和2个文本框.
我希望当用户按下{TAB}时,光标不能移动到第二个TextBox.
用户不能使用{SUBTRACT}输入
谢谢

I have a form and 2 TextBoxes.
I want that, when user press {TAB}, the cursor can''t move to the second TextBox.
And user can''t use {SUBTRACT} to input
Thanks

例如,这将取消显示-":

For example, this will suppress "-":

mytextBox.KeyDown += (sender, eventArgs) => {
    eventArgs.SuppressKeyPress = eventArgs.KeyData == Keys.OemMinus;
};



应该在显示表单之前的任何地方调用此代码,通常在构造函数的末尾(例如,在InitializeComponent之后)调用.
此代码使用lambda形式的匿名方法.此表格在C#v.2.0中不可用.在这种情况下,请使用:



This code should be called anywhere before showing the form, normally at the end of constructor (for example, after InitializeComponent).
This code uses lambda form of anonymous method. This form is not available in C# v.2.0. In this case, use:

mytextBox.KeyDown += delegate(object sender, KeyEventArgs eventArgs) {
    eventArgs.SuppressKeyPress = eventArgs.KeyData == Keys.OemMinus;
};



同样,您可以隐藏任何键集(也可以使用eventArgs.Modifiers来检查Shift,Ctrl或Alt状态).
避免完全抑制TAB:这会违反标准的UI行为(如果您确实想犯此罪行,请与Keys.Tab进行比较,但是更好-不要).



In the same way, you can suppress any key set (also use eventArgs.Modifiers to check Shift, Ctrl or Alt status).
Avoid suppressing TAB by all means: it would violate standard UI behavior (if you really want to commit this crime, compare with Keys.Tab, but better -- don''t).




嗯,您可以使用ascii代码了解按下了哪些键并采取相应的措施.捕获Windows的keyPress事件,或者如果您使用asp.net


well u can use ascii code to know which keys where pressed and take action accordingly. Capture the keyPress event for windows or use javascript if ur using asp.net