vb.net textbox 模糊查询
连上数据库了, 想在textbox中输入文字就出现模糊查询的结果在下面(像下拉的),选中就到textbox中去了。求这代码? textbox?模糊查询
[解决办法]
楼上的这个就可以。
只要在textboxCHANGED事件中动态给ListBox赋值就可以了,选择了一个值之后隐藏ListBox,将值给Textbox.
我的Textbox名字是TxtProName,ListBox名字是ListProName
这个是查数据库将指定列给ListBox赋值的函数:
'根据传入的数据为ListBox加载数据
Private Sub Load_ListProName(ByVal str_txt As String)
Dim strSql As New System.Text.StringBuilder
'如果没有数据则不加载
If str_txt.Trim = "" Then Exit Sub
Try
Using conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\PadicalDb.accdb")
conn.Open()
Using comm As New OleDb.OleDbCommand
strSql.Length = 0
strSql.Append("Select DISTINCT(cPRODUCT_NAME_NOW) from MST_PRODUCT")
strSql.Append(" WHERE cPRODUCT_NAME_NOW LIKE '")
strSql.Append(str_txt.Trim)
strSql.Append("%' ORDER BY cPRODUCT_NAME_NOW")
comm.Connection = conn
comm.CommandText = strSql.ToString
Dim drInfo As OleDb.OleDbDataReader = comm.ExecuteReader
'为ListBox添加列
ListProName.Items.Clear()
If drInfo.Read Then
PnlListProName.Visible = True
ListProName.Items.Add(drInfo("cPRODUCT_NAME_NOW"))
Else
'MsgBox("此产成品不存在,请联系管理人员添加到数据库!")
PnlListProName.Visible = False
Exit Sub
End If
While drInfo.Read
ListProName.Items.Add(drInfo("cPRODUCT_NAME_NOW"))
End While
'ListProName.SelectedIndex = 0
End Using
End Using
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub TxtProName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtProName.TextChanged
'如果不是上下键触发的TextChange
If g_bool_isUpDown = False Then
Load_ListProName(TxtProName.Text.Trim)
End If
End Sub
'listbox的selectindex发生变化就将其填入产品名称中,并屏蔽其TextChanged事件
Private Sub ListProName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListProName.SelectedIndexChanged
g_bool_isUpDown = True
TxtProName.Text = ListProName.SelectedItem.ToString
TxtProName.SelectionStart = Len(ListProName.SelectedItem.ToString)
g_bool_isUpDown = False
End Sub