WinForms:为文本编辑器提供动态建议弹出窗口

WinForms: providing text editor with dynamic suggestions popup

我想为我们的应用程序实现类似于 Google 的搜索栏 行为:用户必须能够以自由文本形式输入用户名,并基于输入的内容数据系统必须根据已经存在的用户名在弹出栏上提供一些建议。


这是简要算法:

我不能使用 devexpress 的 lookupeditds - 它们只允许保留数据源中显示的值 - 即使新值已在 ProcessNewValue 中通过添加到数据源进行处理,

更改事件再次触发,刷新我的数据源覆盖新的唯一值。 现在我期待 Combobox 控件。但是看起来无法在显示建议弹出窗口的同时输入自由文本。

I can't use devexpress's lookup edits - they allow to keep only values, presented in datasource - even if new value has being processed inside ProcessNewValue by adding to datasource,

我相信你在这里错了,因为你可以轻松使用 DevExpress LookUpEdit:

class AutoCompleteLookUpEdit : LookUpEdit {
    List<string> suggestions = new List<string>();
    public AutoCompleteLookUpEdit() {
        Properties.DataSource = suggestions;
        Properties.ImmediatePopup = true;
    }
    protected override void ProcessFindItem(KeyPressHelper helper, char pressedKey) {
        suggestions.Clear();
        // add search suggestions here depending on helper.Text value
        suggestions.Add("google");
        suggestions.Add("devexpress");
        // ...
        base.ProcessFindItem(helper, pressedKey);
    }
}

查看 How to create an editor with a dynamic autocomplete list 了解详细示例。

P.S。您可以使用 AcceptEditorTextAsNewValue 属性 来控制查找是否接受输入的文本作为有效值,即使它不属于基础数据源。