C# 通过键入选择组合框中的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10187963/
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
Selecting an item in comboBox by typing
提问by prabhuK2k
I've a combobox that has hundreds item in it. User must be able to type the text into the combobox . While the user is typing the text, the item that starting with the typed value must be selected or listed. The user must be able type continuously. My ComboBox DropDownStyle is DropDownList
我有一个组合框,里面有数百个项目。用户必须能够在组合框中键入文本。在用户键入文本时,必须选择或列出以键入的值开头的项目。用户必须能够连续打字。我的 ComboBox DropDownStyle 是 DropDownList
Eg: While selecting a name in comboBox by typing, it only allows one letter. So if I type "A" it will jump to the first letter starting with "A". when i type continuously the combo box selected item changes according to the current keypress. if i press "As", combobox viewing the items starting with "s".
例如:通过键入在组合框中选择名称时,它只允许一个字母。所以如果我输入“A”,它会跳到以“A”开头的第一个字母。当我连续输入时,组合框所选项目会根据当前按键发生变化。如果我按“As”,组合框会查看以“s”开头的项目。
I'm using Win Forms.
我正在使用 Win Forms。
thanks in advance..
提前致谢..
采纳答案by prabhuK2k
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
回答by Likurg
You will have to hook up to the TextChanged event. When the text changes, filter the list (using a DataView) and take the text of the first result, setting the text of the combo box to that. You would have to have a check in your handler of course, to determine whether or not to handle the event (when you change the text, another TextChanged event would be fired). Of course, you also want to highlight the text that they typed in, and place the caret at the appropriate position.
您必须连接到 TextChanged 事件。当文本更改时,过滤列表(使用 DataView)并获取第一个结果的文本,将组合框的文本设置为该文本。当然,您必须检查您的处理程序,以确定是否处理该事件(当您更改文本时,将触发另一个 TextChanged 事件)。当然,您还希望突出显示他们输入的文本,并将插入符号放在适当的位置。

