共享一个输入字母定位汉字的下拉列表源代码
Public Class MyChsFstChrCombo
'使用Collection保存和Combox同步的一个数据集合
Private mColItemKey As New Collection()
Private mColFstChr As New Collection
Private mColItemText As New Collection
Private mStrSelText As String
Private mBlnIsOpenTextChange As Boolean = True
'增加Item,同时增加对应的一个数据
Public Sub AddItem(ByVal iStrItem As String, ByVal iObjItemData As Object)
Dim iIndex As Int64
iIndex = cboLst.Items.Add(iStrItem)
'数据集合采用Item的Index的ToString作为关键字
mColItemKey.Add(iObjItemData, iIndex.ToString)
mColFstChr.Add(GetFirstLetter(iStrItem, False), iIndex.ToString)
mColItemText.Add(iStrItem, iIndex.ToString)
End Sub
Private Sub cboInput_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboInput.Click
If Not cboLst.DroppedDown Then cboLst.DroppedDown = True
End Sub
'返回当前选中的Item对应的数据
Public Function GetSelectItemData() As Object
Try
'没有选择的时候,通过文本匹配获得相应的数据
Dim i As Int32
For i = 0 To mColItemText.Count - 1
If Me.Text = mColItemText(i.ToString) Then Return mColItemKey(i.ToString)
Next
Return -1
Catch ex As Exception
Return -1
End Try
End Function
Public Function GetItemData(ByVal iIndex As Int64) As Object
Try
'没有选择的时候,通过文本匹配获得相应的数据
Dim i As Int32
For i = 0 To mColItemText.Count - 1
If cboLst.Items(iIndex) = mColItemText(i.ToString) Then Return mColItemKey(i.ToString)
Next
Return -1
Catch ex As Exception
Return -1
End Try
End Function
Public Function SetItemDataSelect(ByVal iItemData As Object) As Boolean
Try
Dim i As Int32
For i = 0 To mColItemKey.Count - 1
If mColItemKey(i.ToString) = iItemData Then
cboInput.Text = cboLst.Items(i)
MyBase.Refresh()
Exit Function
End If
Next
MyBase.Text = " "
Catch ex As Exception
End Try
End Function
Public Sub Clear()
cboLst.Text = " "
cboInput.Text = " "
mColItemKey = Nothing
mColItemKey = New Collection
mColFstChr = Nothing
mColFstChr = New Collection
mColItemText = Nothing
mColItemText = New Collection
cboLst.Items.Clear()
End Sub
Private Sub cboInput_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboInput.KeyUp
mBlnIsOpenTextChange = False
If e.KeyCode = Keys.Enter Then
cboInput.Text = mStrSelText
cboInput.SelectionLength = cboInput.Text.Length
If cboLst.DroppedDown Then cboLst.DroppedDown = False
End If
If e.KeyCode = Keys.Down Then
If Me.cboLst.SelectedIndex < Me.cboLst.Items.Count - 1 Then
cboLst.SelectedIndex = cboLst.SelectedIndex + 1
mStrSelText = cboLst.Items(cboLst.SelectedIndex.ToString)
End If
End If
If e.KeyCode = Keys.Up Then
If Me.cboLst.SelectedIndex > 0 Then
cboLst.SelectedIndex = cboLst.SelectedIndex - 1
mStrSelText = cboLst.Items(cboLst.SelectedIndex.ToString)
End If
End If
mBlnIsOpenTextChange = True
End Sub
Private Sub cboInput_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboInput.TextChanged
'判断是否处理文本变化
If Not mBlnIsOpenTextChange Then Exit Sub
'If cboLst.DroppedDown Then cboLst.DroppedDown = False
cboLst.Items.Clear()
Dim i As Int16
For i = 0 To mColFstChr.Count - 1
If Strings.InStr(mColFstChr(i.ToString), cboInput.Text) > 0 Then
'存在字符串
cboLst.Items.Add(Me.mColItemText(i.ToString))
End If
Next
If cboLst.Items.Count > 0 Then
cboLst.DroppedDown = True
cboLst.SelectedIndex = 0
mStrSelText = cboLst.Items(cboLst.SelectedIndex.ToString)
End If
End Sub
Private Sub cboLst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboLst.Click
End Sub
Private Sub cboLst_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboLst.TextChanged
mBlnIsOpenTextChange = False
If Not cboLst.DroppedDown Then
mStrSelText = cboLst.Text
cboInput.Text = mStrSelText
End If
mBlnIsOpenTextChange = True
End Sub
End Class
[解决办法]
看得三昏三昏的
能不能说的在详细些啊