简体   繁体   English

覆盖文本框中的 Ctrl 键?

[英]Override Ctrl key in TextBox?

Is it possible to have a textbox ignore Ctrl + character function and reassign a different use for the ctrl key?是否可以让文本框忽略Ctrl +字符功能并为 ctrl 键重新分配不同的用途?

Situation: I have multiple text boxes on a Form.情况:我在一个表单上有多个文本框。 Ideally I would like the textboxes to jump from one textbox to another after enter in a single character.理想情况下,我希望文本框在输入单个字符后从一个文本框跳转到另一个文本框。 However the textbox can have more than one input, in this case I want the user to hold ctrl to stay on the textbox while they enter in multiple characters.然而,文本框可以有多个输入,在这种情况下,我希望用户在输入多个字符时按住ctrl以保持在文本框上。

Essentially, I want my ctrl (if held down) to stop the textbox from jumping.本质上,我希望我的ctrl (如果按住)阻止文本框跳跃。

I've tried using a combination of OnKeyDown , OnKeyUp and TextChanged event to handle when the Ctrl key is pressed, but in the end, I can't enter any data once the Ctrl key is held down.我尝试使用OnKeyDownOnKeyUpTextChanged事件的组合来处理按下Ctrl键时的事件,但最后,一旦按住 Ctrl键,我就无法输入任何数据。 Is there a way around this?有没有解决的办法?

The reason for this is because 95 percent of the time its always 1 character.这样做的原因是因为 95% 的时间它总是 1 个字符。 Therefore to quickly traverse through the textboxes, the user would hit a single character.因此,为了快速遍历文本框,用户将点击单个字符。 However in the unlikelihood that the user wants to enter in 2 digits, they can hold Ctrl (So it doesn't jump to the next textbox) and enter in 2 digits as desired.然而,在用户想要输入 2 位数字的可能性中,他们可以按住Ctrl (因此它不会跳到下一个文本框)并根据需要输入 2 位数字。

Like the commenters said, there's probably a better way of doing this, in a UI sense.就像评论者所说的那样,从用户界面的角度来看,可能有更好的方法来做到这一点。 That you would press CTRL to prevent switching textboxes is weird behavior.你会按CTRL来防止切换文本框是一种奇怪的行为。 Having said that, using the following in KeyDown should get you what you were trying to do.话虽如此,在 KeyDown 中使用以下内容应该可以帮助您完成您想要做的事情。

if (!(e.Control && char.IsLetterOrDigit((char) e.KeyCode)))
{
    return;
}

txtBox.Text += (new KeysConverter()).ConvertToString(e.KeyCode);
e.Handled = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM