vb.net 实现ComboBox输入字符自动补充字符
实现ComboBox输入字符自动补充后面的字符比如ComboBox中有数据:baacassddf输入a显示数据里带a的ba,ac,...求完整代码...
实现ComboBox输入字符自动补充后面的字符
比如ComboBox中有数据:
ba
ac
as
sd
df
输入a显示数据里带a的ba,ac,... 求完整代码 展开
比如ComboBox中有数据:
ba
ac
as
sd
df
输入a显示数据里带a的ba,ac,... 求完整代码 展开
1个回答
展开全部
Public Sub AutoComplete(ByVal cmb As ComboBox, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If cmb.DataSource Is Nothing Then
Return
End If
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
Return
End If
Dim strFindStr As String = ""
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Back) Then
If (cmb.SelectionStart >= cmb.Text.Length) Then
If cmb.Text.Length > 0 Then
strFindStr = cmb.Text.Substring(0, cmb.Text.Length - 1)
End If
Else
If cmb.SelectionStart > 0 Then
strFindStr = cmb.Text.Substring(0, cmb.SelectionStart - 1)
End If
End If
e.Handled = False
Else
If (cmb.SelectionLength = 0) Then
strFindStr = cmb.Text + e.KeyChar
Else
If (cmb.SelectionStart >= cmb.Text.Length) Then
strFindStr = e.KeyChar
Else
If cmb.SelectionStart > 0 Then
strFindStr = cmb.Text.Substring(0, cmb.SelectionStart - 1) + e.KeyChar
Else
strFindStr = e.KeyChar
End If
End If
End If
End If
Dim intIdx As Integer = -1
Dim dv As DataView
If TypeOf (cmb.DataSource) Is DataTable Then
dv = CType(cmb.DataSource, DataTable).DefaultView
If strFindStr <> "" Then
dv.RowFilter = cmb.DisplayMember & " Like '%" & strFindStr & "%'"
Else
dv.RowFilter = ""
End If
cmb.DataSource = dv
cmb.SelectedIndex = -1
cmb.Text = strFindStr
Else
dv = CType(cmb.DataSource, DataView)
If strFindStr <> "" Then
dv.RowFilter = cmb.DisplayMember & " Like '%" & strFindStr & "%'"
Else
dv.RowFilter = ""
End If
cmb.DataSource = dv
cmb.SelectedIndex = -1
cmb.Text = strFindStr
End If
cmb.SelectionStart = strFindStr.Length
e.Handled = True
End Sub
Private Sub comboBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles comboBox1.KeyPress
AutoComplete(sender, e)
End Sub
更多追问追答
追问
你发的什么
追答
代码啊,不是你要求自动匹配的吗?你都不试试?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询