Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
810 views
in Technique[技术] by (71.8m points)

winforms - How to make autocomplete on a TextBox show suggestions when empty

I am using the AutoComplete properties on a textbox (actually a ToolStripTextBox). This is working fine except it doesn't show until I type at lease one character. How do I make it so the suggestions are shown even if the textbox is empty?

Mode = Suggest
Source = CustomSource

Source set progamatically and limited to 10 items

Alternatively if someone knows how to force the suggestions to show programatically on the OnEnter event that might be a solution

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Take into account that this is a hack. I managed to solve that problem and the lack of API functionality doing a trivial and nasty thing. I'll show you this with code:

    dim source as AutoCompleteStringCollection = new AutoCompleteStringColection()
    dim values() as String = new String() {" Monday", _
                                           " Tuesday", _
                                           " Wednesday", _
                                           " Thursday", _
                                           " Friday", _
                                           " Saturday", _
                                           " Sunday" }
    TextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
    TextBox1.AutoCompleteCustomSource = source

That is, prepend a whitespace to every string in the autocomplete list. Then, it's your knowlodge about that fact and use it for your convenient objective.

For example, you could add a whitespace in the TextBox when clicked, focused, etc. (Note that this could be done with any character. The idea is to know that every string in the autocomplete list begins with the same character)

You MUST be aware of that. In fact, consider extending TextBox form and manage the correct trimming of the inputed string.

Again, called this recommended or not is at your own decision. What this answer tends to do, is to solve the problem of wanting a TextBox drops down a suggestion list without starting typing with the restrictions of the API, also called, a workaround or ugly-hack.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...