Newsgroup: it.comp.lang.visual-basic
Titolo: Autocompletamento in combo

Salve a tutti mi servirebbe fare un autocompletamento in una combobox.
Sapete qualche metodo per fare ciň??

 Sapete qualche metodo per fare ciň??
' Questo va in un modulo
Public Sub AutoCompleteCombo(Combo1 As ComboBox, KeyAscii As Integer)
    Dim lCnt       As Long 'Generic long counter
    Dim lMax       As Long
    Dim sComboItem As String
    Dim sComboText As String 'Text currently in combobox
    Dim sText      As String 'Text after keypressed
    Dim miSelStart As Integer
    On Error GoTo errore
    miSelStart = Combo1.SelStart 'Set the entry point of the selected text
    If KeyAscii = 13 Or KeyAscii = 27 Then Exit Sub 'Charage Return or
escape
    With Combo1
        lMax = .ListCount - 1 'The number of values in the list
        sComboText = .Text 'The current text in the list
        '**********************************************
        'This block either removes or adds a character
        '***********************************************
        If KeyAscii = 8 Then 'Backspace
            'If there is one character left, remove the text
            If Len(Combo1.Text) = 1 Or Len(Combo1.Text) = 0 Then
                Combo1.Text = ""
                miSelStart = 0
                Exit Sub
            'There is one character and seltext (our matched text)
            ElseIf Len(Combo1.Text) - (Len(Combo1.Text) - miSelStart) = 1
Then
                Combo1.Text = ""
                miSelStart = 0
                Exit Sub
            End If
            'An ordinary backspace, decrement 1 character
            Combo1.Text = Left(sComboText, miSelStart - 1)
            sText = Left(sComboText, miSelStart - 1) 'reset our sText var
sText = combo1.text??
        Else
            'A char other than backspace, return or escape was pressed
            sText = Left(sComboText, miSelStart) & Chr(KeyAscii) 'Increment
our string with the new char
        End If
        KeyAscii = 0 'Reset key pressed
        '**********************************************
        'This block performs our lookup
        '**********************************************
        For lCnt = 0 To lMax
            sComboItem = .List(lCnt) 'Current item
            If UCase(sText) = UCase(Left(sComboItem, _
                                         Len(sText))) Then 'A match was
found
                '.ListIndex = lCnt 'Not sure why this is needed
                .Text = sComboItem
                .SelStart = Len(sText) 'Start the highlighting after
manually entered text
                .SelLength = Len(sComboItem) - (Len(sText)) 'Highlight to
the end of the text
                Exit For
            Else
                .Text = sText 'Set the text value = the new text
                .SelStart = Len(.Text) 'Set selstart to the end of the
string
            End If
        Next 'lCnt
    End With
Exit Sub
errore:
    If Err.Number <> 5 Then
        MessageBox "E' avvenuto un errore: " & Err.Number & " " &
Err.Description, OKOnly, Critical
    End If
End Sub
*************
Fine Modulo
************
Per usarlo invece fai:
Private Sub Combo1_KeyPress(KeyAscii As Integer)
 Call AutoCompleteCombo(Combo1, KeyAscii)
End Sub
bye
-- 
Esistono 10 tipi di persone: chi conosce il binario e chi no.
Gestione Ottica - Gestionale per Ottici e Programmi Gratuiti su:
http://www.nicolaottomano.it