Reputation: 159
I am trying to add an autocomplete box to my winform.
The GetDatabaseContent()
works fine when I apply it to a combo box
but I want to add it to a textbox
.
The issue my current code is that is brings the contents of my Datatable
as a single line rather than a list.
Any help would be great.
Private Sub frmUser_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim mystring
Dim dt As DataTable = GetDatabaseContent()
mystring = FetchTable(dt)
Dim newstr As New AutoCompleteStringCollection
newstr.Add(mystring)
txtbox.AutoCompleteCustomSource = newstr
End Sub
Function FetchTable(dt As DataTable)
Dim mystring = ""
For Each dr As DataRow In dt.Rows
mystring &= dr.Item(0).ToString
Next
Return mystring
End Function
Upvotes: 2
Views: 1514
Reputation: 81610
You are only creating a single line of text to use as the data source, so try just adding each row of text to the collection instead:
Dim newstr As New AutoCompleteStringCollection
For Each dr As DataRow In dt.Rows
newstr.Add(dr.Item(0).ToString)
Next
TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
TextBox1.AutoCompleteCustomSource = newstr
Upvotes: 2