C# 覆盖 WinForms 中的选项卡行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override tab behavior in WinForms
提问by Robert H?glund
I have a UserControl that consists of three TextBoxes. On a form I can have one or more or my UserControl. I want to implement my own tab behavior so if the user presses Tab in the second TextBox I should only move to the third TextBox if the the second TextBox has anything entered. If nothing is entered in the second TextBox the next control of the form should get focus as per the normal tab behavior. If the user hasn't entered anything in the first or second TextBox and the presses tab there is this special case where a control on the form should be skipped.
我有一个由三个 TextBox 组成的 UserControl。在一个表单上,我可以有一个或多个或我的 UserControl。我想实现我自己的选项卡行为,因此如果用户在第二个 TextBox 中按下 Tab,如果第二个 TextBox 输入了任何内容,我应该只移动到第三个 TextBox。如果在第二个 TextBox 中没有输入任何内容,则表单的下一个控件应该按照正常的选项卡行为获得焦点。如果用户没有在第一个或第二个 TextBox 和 presses 选项卡中输入任何内容,则存在这种特殊情况,应跳过表单上的控件。
By using the ProcessDialogKey I have managed to get it work kind of ok but I still have one problem. My question is if there is a way to detect how a WinForms control got focus since I would also like to know if the my UserControl got focus from a Tab or Shift-Tab and then do my weird stuff but if the user clicks the control I don't want to do anything special.
通过使用 ProcessDialogKey,我设法让它正常工作,但我仍然有一个问题。我的问题是,是否有一种方法可以检测 WinForms 控件如何获得焦点,因为我还想知道我的 UserControl 是否从 Tab 或 Shift-Tab 获得焦点,然后执行我奇怪的操作,但是如果用户单击控件我不想做任何特别的事情。
采纳答案by Rob Thomas
As a general rule, I would say overriding the standard behavior of the TAB key would be a bad idea. Maybe you can do something like disabling the 3rd text box until a valid entry is made in the 2nd text box.
作为一般规则,我会说覆盖 TAB 键的标准行为是一个坏主意。也许您可以执行一些操作,例如禁用第三个文本框,直到在第二个文本框中输入有效条目。
Now, having said this, I've also broken this rule at the request of the customer. We made the enter key function like the tab key, where the enter key would save the value in a text field, and advance the cursor to the next field.
现在,话虽如此,我也应客户的要求打破了这条规则。我们使回车键功能类似于 tab 键,其中回车键会将值保存在文本字段中,并将光标移动到下一个字段。
回答by TheSmurf
I don't think there's a built-in way that you could do it. All of the WinForms focus events (GotFocus,LostFocus,Enter,Leave) are called with empty EventArgs parameters, which will not give you any additional information.
我认为没有内置的方法可以做到这一点。所有 WinForms 焦点事件(GotFocus、LostFocus、Enter、Leave)都是使用空的 EventArgs 参数调用的,它不会为您提供任何附加信息。
Personally, I would disable the third textbox, as Rob Thomas said. If you're determined to do this, though, it wouldn't be difficult to set up a manual (read: hackish) solution. Once the tab key is pressed (if the focus is on the second textbox), set a variable inside your form. If the next object focused is then the third textbox, then you know exactly how it happened.
就个人而言,正如 Rob Thomas 所说,我会禁用第三个文本框。但是,如果您决心这样做,则设置手动(阅读:hackish)解决方案并不困难。按下 Tab 键后(如果焦点位于第二个文本框),请在表单中设置一个变量。如果下一个焦点对象是第三个文本框,那么您就知道它是如何发生的。
回答by Patrik Svensson
I agree with DannySmurf. Messing with the tab order might give you hell later on if the requirements for the application change.
我同意 DannySmurf 的观点。如果应用程序的要求发生变化,那么乱用 Tab 键顺序可能会让您在以后遇到麻烦。
Another thing that you could do is to implement some kind of wizard for the user to go through.
您可以做的另一件事是为用户实现某种向导。
回答by Robert H?glund
The reason for this odd tab behavior is all about speed in the input process. It was really good to get some input, I hadn't thought about disabling a textbox but that could actually work. But using the Enter key to accept the input hadn't even crossed my mind. That will work so much better. The user can enter the numbers and then press enter to accept the input and the next possible textbox will be the active one. It's like having the cake and eating it too, The speed factor is there since when using the enter key no unnecessary tabing must be done to get to the correct field and using the enter key next to the numeric keyboard makes it really smooth.
这种奇怪的制表符行为的原因完全在于输入过程的速度。获得一些输入真的很好,我没想过禁用文本框,但这实际上可以工作。但是使用 Enter 键来接受输入我什至没有想过。这样效果会好很多。用户可以输入数字,然后按 Enter 接受输入,下一个可能的文本框将是活动文本框。这就像吃蛋糕和吃蛋糕一样,速度因素是存在的,因为在使用回车键时,不必进行不必要的跳格以到达正确的字段,并且使用数字键盘旁边的回车键使其非常流畅。
Thanks for the input!
感谢您的输入!
回答by Bevan
Better than disabling controls, try monkeying around with TabStop - if this is false, the control will be simply skipped when tabbing.
比禁用控件更好,尝试使用 TabStop 胡闹 - 如果这是错误的,则在使用 TabStop 时将简单地跳过控件。
I'd also suggest that the Changed event of the TextBox is the place to be updating TabStop on the other controls.
我还建议 TextBox 的 Changed 事件是在其他控件上更新 TabStop 的地方。
I've done something similar to this with a login control, where users could enter either a username or an email address (in separate fields), plus their password, and tabStop is what I used to get the job done.
我用登录控件做了类似的事情,用户可以在其中输入用户名或电子邮件地址(在单独的字段中),加上他们的密码,而 tabStop 是我用来完成工作的。