|
|
|
|
|
Dicas
|
|
Visual Basic (ActiveX/Controles/DLL)
|
|
|
Título da Dica: Autocompletar sem módulos e sem API
|
|
|
|
Postada em 10/2/2003 por Ethan Hunt
Crie um Form com TextBox e um LIstBox(que será a base de dados) e insira o seguinte código:
Option Explicit Private Const LB_FINDSTRING = &H18F Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Sub Form_Load() List1.AddItem "Banana" List1.AddItem "Laranja" List1.AddItem "Limão" List1.AddItem "Maça" List1.AddItem "Melancia" List1.AddItem "Morango" List1.AddItem "Pera" End Sub
Private Sub Text1_Change() Dim pos As Long List1.ListIndex = SendMessage(List1.hWnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
If List1.ListIndex = -1 Then pos = Text1.SelStart Else pos = Text1.SelStart Text1.Text = List1 Text1.SelStart = pos Text1.SelLength = Len(Text1.Text) - pos End If
End Sub Private Sub Text1_KeyDown(KeyCode As _ Integer, Shift As Integer) On Error Resume Next If KeyCode = 8 Then 'Backspace If Text1.SelLength <> 0 Then Text1.Text = Mid$(Text1, 1, _ Text1.SelStart - 1) KeyCode = 0 End If ElseIf KeyCode = 46 Then 'Del If Text1.SelLength <> 0 And Text1.SelStart <> 0 Then Text1.Text = "" KeyCode = 0 End If End If End Sub
A medida que o usuário for digitando no textbox ele auto completará com o conteúdo do listbox caso exista alguma palavra com as letras digitadas.
|
|
|
|
|