Vb.net + AutoComplete in textboxes

后端 未结 2 1860
时光取名叫无心
时光取名叫无心 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.

    0 讨论(0)
  • 2021-01-13 13:51

    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 讨论(0)
提交回复
热议问题