How to make autocomplete on a TextBox show suggestions when empty

后端 未结 4 1744
囚心锁ツ
囚心锁ツ 2020-12-11 09:15

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

4条回答
  •  佛祖请我去吃肉
    2020-12-11 09:45

    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.

提交回复
热议问题