如何更改组合框键盘输入行为
(Visual Studio 2010中的WinForms项目,.NET 4,C#)
我有一个DropDownList样式的ComboBox,其中包含一组固定的项目,这些项目是从1到40的整数,即ComboBox用于输入从1到40的数字40.
现在当用户使用键盘输入一个新值,比如“1”,而控件的当前值为“2”时,ComboBox 首先跳转到“10”,而我希望它跳转到“1”,并且仅在第二次按键时跳转到以“1”开头的第二个条目,即“10”。
有没有一种简单的方法可以改变这种行为?
(WinForms Project in Visual Studio 2010, .NET 4, C#)
I have a ComboBox in DropDownList style with a fixed set of items which are the integral numbers from 1 to 40, i. e., the ComboBox is used to input a number from 1 to 40.
Now when users use the keyboard to enter a new value, say "1", and the current value of the control is "2", the ComboBox first jumps to "10", whereas I would prefer it to jump to "1" and only on the second keypress jump to the second entry beginning with "1", which would be "10".
Is there a simple way to change this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为没有“简单的方法”。您需要构建自定义 ComboBox 类并覆盖 控件.ProcessKeyEventArgs 方法。
I don't think there is a "simple way". You need build a custom ComboBox class and override the Control.ProcessKeyEventArgs Method.
ComboBox
从当前选定的项目开始进行增量搜索。输入 1 0 将引导您进入以“10”开头的条目,无论选择哪一个。如何用前导零填充所有条目,以便用户为第一个条目键入“01”?
这也将确保条目正确排序(您是否应该对列表项进行排序,以及它们是否应该在数字字符后面包含文本,迟早可能会出现这种情况;))。
ComboBox
does an incremental search, starting at the currently selected item. Typing 1 0 will lead you to the entry starting with "10", no matter which one was selected.How about padding all entries with leading zeros, so the user would type "01" for the first entry?
That also would make sure that the entries are sorted correctly (should you ever sort the list items, and should they contain text behind the numerical characters, as probably is the case sooner or later ;) ).