Vb.net + AutoComplete in textboxes

后端 未结 2 1859
时光取名叫无心
时光取名叫无心 2021-01-13 13:14

So I was reading a bit on AutoComplete of textboxes in VB.NET, but I can\'t really understand where these are stored? Is it a fully built in feature, or do I have to write s

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-13 13:42

    You would have to add new entries to your auto-complete data source manually... which makes sense, when you think about it: How is Windows Forms supposed to know when a new entry should be added to the list of suggestions and when the text entered is only something temporary?

    You could add new values e.g. when validation of the input field happens, or when the user presses an OK / Apply button, or whatever fits your need best. But you will have to do this yourself.

    The properties you've already discovered are the right ones.

    Dim suggestions As New List(Of String)
    suggestions.Add("Abba")
    suggestions.Add("Nirvana")
    suggestions.Add("Rolling Stones")
    ...
    textBox.AutoCompleteSource = suggestions
    

    You can bind AutoCompleteSource to almost anything; this is very similar to data-binding. One thing to keep in mind is that if you're adding new entries to the auto-complete data source, the UI control might not immediately notice if your data source doesn't implement the INotifyCollectionChanged interface.

提交回复
热议问题