简体   繁体   English

C#文本框自动完成功能:如何提供自定义的“匹配”功能?

[英]C# Textbox Autocomplete: How to provide custom 'matching' function?

I'm using C# TextBox with AutoComplete via a Custom Source of a collection of names, eg John Smith, Mary Jane, etc .. The problem is that if I type in 'John', I do see 'John Smith' in the dropdown. 我正在通过名称的自定义来源(例如John Smith,Mary Jane等)使用具有自动完成功能的C#TextBox。问题是,如果我键入“ John”,则在下拉列表中会看到“ John Smith” 。 But if I start typing 'Smith', I no longer see 'John Smith' in the dropdown. 但是,如果我开始输入“ Smith”,则在下拉列表中将不再看到“ John Smith”。 I'd like to change this, and I think I would have to extend the TextBox class and provide my own function for matching data. 我想更改此设置,并且我想必须扩展TextBox类并提供我自己的函数以匹配数据。 I just don't know which TextBox function would need to be overwritten. 我只是不知道哪个TextBox函数需要被覆盖。

Secondly, another related issue I'd like to solve this is: The dropdown text is always left aligned. 其次,我要解决的另一个相关问题是:下拉文本始终保持对齐。 How can I make it center aligned ? 如何使它居中对齐?

There is no directly way to do it because AutoCompleteCustomSource only allows prefix matching based on your input string. 没有直接的方法,因为AutoCompleteCustomSource仅允许根据您的输入字符串进行前缀匹配。

From TextBox.AutoCompleteCustomSource Property TextBox.AutoCompleteCustomSource属性

Use the AutoCompleteCustomSource, AutoCompleteMode, and AutoCompleteSource properties to create a TextBox that automatically completes input strings by comparing the prefix being entered to the prefixes of all strings in a maintained source. 使用AutoCompleteCustomSource,AutoCompleteMode和AutoCompleteSource属性来创建一个文本框,通过比较前缀被输入到所有字符串的前缀在保持源自动完成输入字符串。

Best option seems to override your OnTextChanged event of your TextBox . 最好的选择似乎是重写TextBox OnTextChanged事件 The question below has a good example about it; 下面的问题有一个很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM