通过键入前几个字符跳转到列表框项目

2022-01-12 00:00:00 user-interface winapi mfc

我有一个项目列表(可能很大),用户必须从中选择一个.我想让用户输入所需项目的前几个字母,以跳转到列表中的正确位置.默认情况下,每次按键都会跳转到以该字母开头的第一项,因此您无法键入前 几个 字母.有没有直接的方法可以做到这一点?任何 CodeProject 或其他此类示例?

I have a list of items (potentially large) from which the user must select one. I'd like to allow the user to type the first few letters of the desired item to jump to the correct place in the list. By default, each keypress jumps to the first item starting with that letter, so you can't type the first several letters. Is there any straightforward way to do this? Any CodeProject or other such example?

我已经找了好几个小时,找到了任意数量的 IAutocomplete 样本,但这在这里无济于事,因为我需要保证结果在列表中.

I've looked for hours, and found any number of samples for IAutocomplete, but that won't help here because I need to guarantee that the result is in the list.

我能想到的唯一方法是从 CListBox 派生,自己捕获击键,找到项目,运行计时器,以便在足够的暂停后新的击键将开始新的搜索......因为我不是 MFC 运动员,这令人生畏.非常感谢任何提示.

The only way I can think to do this is to derive from CListBox, capture the keystrokes myself, find the item, run a timer so that new keystrokes after a sufficient pause will start a new search... since I'm not an MFC jock, this is daunting. Any tips much appreciated.

一个澄清说明:我的最终目标实际上是为 DropDownList 样式的 ComboBox 获得这种键盘行为(即没有编辑框).缺少编辑框排除了大多数自动完成代码,并且需要 ComboBox 功能意味着我不能单独使用 CListCtrl.

One clarifying note: my ultimate goal is actually to get this keyboard behavior for a ComboBox of DropDownList style (i.e. no edit box). The lack of an edit box rules out most autocomplete code, and the need for ComboBox functionality means I can't use CListCtrl by itself.

推荐答案

在经历了很多不必要的痛苦之后,我发现真正正确的答案就是使用 LBS_SORT.只需指定此样式,基本的香草列表框就支持我想要的增量搜索键盘快捷键样式.如果没有 LBS_SORT(或组合框的 CBS_SORT),您会得到令人讨厌且几乎无用的仅跳转到第一个字母的行为.我没有尝试 LBS_SORT,因为无论如何我的列表内容都是按排序顺序添加的.

After much unnecessary pain, I've discovered that the real correct answer is simply to use LBS_SORT. Simply by specifying this style, the basic vanilla listbox supports the incremental search keyboard shortcut style I wanted. Without LBS_SORT (or CBS_SORT for a combobox), you get the irritating and almost-useless jump-to-first-letter-only behavior. I didn't try LBS_SORT because my list contents were added in sorted order anyway.

因此,研究自定义控件等的十几个小时都是徒劳的,因为 Microsoft 文档在 LBS_SORT 的描述中没有提到这一重要的行为差异!!

So the dozen or so hours of investigating custom controls, etc., all for naught because the Microsoft documentation makes no mention of this important behavioral difference in the description of LBS_SORT!!

感谢所有做出贡献的人.

Thanks to everyone who contributed.

相关文章