我正在尝试为我的winform添加一个自动完成框。
GetDatabaseContent()
将其应用于combo box
时可以正常工作,但我想将其添加到textbox
。
我当前代码的问题是将Datatable
的内容作为单行而不是列表。
任何帮助都会很棒。
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
答案 0 :(得分:2)
您只创建一行文本作为数据源,因此请尝试将每行文本添加到集合中:
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