开发者

Vb.net + AutoComplete in textboxes

开发者 https://www.devze.com 2023-01-24 18:02 出处:网络
So I was reading a bit on AutoComplete of textboxes in VB.NET, but I can\'t really understand where开发者_StackOverflow these are stored? Is it a fully built in feature, or do I have to write some cod

So I was reading a bit on AutoComplete of textboxes in VB.NET, but I can't really understand where开发者_StackOverflow these are stored? Is it a fully built in feature, or do I have to write some code for it to work? I've found the AutoCompleteMode and AutoCompleteSource properties of textboxes. But I want to append whatever I've written in the textbox to the autocomplete source. Do I connect the source to My.Settings or something? Wouldn't I have to use an array as well? Any help would be appreciated :)


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.


first create the list to use as the custom source.

Dim MySource As New AutoCompleteStringCollection()

and then set the property of the textbox

With MyTextbox
   .AutoCompleteCustomSource = MySource
   .AutoCompleteMode = AutoCompleteMode.SuggestAppend
   .AutoCompleteSource = AutoCompleteSource.CustomSource
End With

put this code in eventlistener you use for validating the input field, e.g. btnOK.Click

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    MySource.Add(txtinput.text)
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消