|
Быстрый поиск элемента в списке |
|
|
Данный пример позволяет по вводимым буквам получить определенный элемент в ComboBox'е.
Разместите на форме элемент ComboBox. Запустите проект на выполнение и попробуйте набрать в ComboBox'е "ext".
Как вы понимаете, за быстрый поиск отвечает событие Combo1_KeyUp. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias
"RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long)
As Long
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias
"RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As
String, ByVal cbName As Long) As Long
Function GetAllExts() As Variant
Dim lRegResult As Long
Dim lCounter As Long
Dim hCurKey As Long
Dim strBuffer As String
Dim lDataBufferSize As Long
Dim intZeroPos As Integer
lCounter = 0
lRegResult = RegOpenKey(&H80000000, "", hCurKey)
Do
lDataBufferSize = 255
strBuffer = String(lDataBufferSize, " ")
lRegResult = RegEnumKey(hCurKey, lCounter, strBuffer, lDataBufferSize)
If lRegResult = 0& Then
intZeroPos = InStr(strBuffer, Chr$(0))
If Left(strBuffer, 1) = "." Then
Form1.Combo1.AddItem LCase(Right(strBuffer, Len(strBuffer) - 1))
End If
lCounter = lCounter + 1
Else
Exit Do
End If
Loop
End Function
Private Sub Form_Load()
GetAllExts
End Sub
Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim st As Long
If KeyCode <> vbKeyReturn And KeyCode <> vbKeyBack And KeyCode <>
vbKeySpace Then
st = Len(Combo1.Text)
If st < 3 Then
For X = 0 To Combo1.ListCount - 1
If Left(Combo1.List(X), st) = Combo1.Text Then
Combo1.ListIndex = X
Combo1.SelStart = st
Combo1.SelLength = Len(Combo1.Text) - st
Exit For
End If
Next X
End If
End If
End Sub
|
|
|
|
|
|
|