Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Match text box input with List View

Status
Not open for further replies.

Error7

Programmer
Jul 5, 2002
656
0
16
GB
I don't even know if I can explain this clearly, let alone write the code to achieve it.
I have a hidden List View that contains names and telephone numbers.
Using a text box I can start to enter a persons name, loop through the contents of the List View and if a match is found, display the full name in a seperate text box or label.
What I would like to do is to display the match found from the List View in the same text box that I am typing in. e.g. When finding a name in a mobile phone's address book.


[gray]Experience is something you don't get until just after you need it.[/gray]
 
Something like the following, assuming a form with a textbox, a listbox and a listview:

Code:
[blue]Option Explicit

Private ListHeight As Long

Private Sub Form_Load()
    ListView1.ListItems.Add , , "Mike"
    ListView1.ListItems.Add , , "Mirror"
    ListView1.ListItems.Add , , "Spoon"
    ListView1.ListItems.Add , , "Cliff"
    ListView1.ListItems.Add , , "Spore"
    ListView1.Visible = False
    
    List1.Appearance = 0
    List1.BackColor = &H80000018  [green]'tooltip color[/green]
    List1.Move Text1.Left, Text1.Top + Text1.Height, Text1.Width, 0
    
    ListHeight = List1.Height [green]'this trick only works if List1.IntegralHeight = True, which can only be set at design time[/green]
    
    List1.Visible = False
   
End Sub

Private Sub List1_Click()
    Text1.Text = List1
    Text1.SelStart = Len(Text1.Text)
    List1.Visible = False
End Sub

Private Sub Text1_Change()
    If Text1.Text <> "" Then
        PopulateList List1
    Else
        Clearlist List1
    End If
End Sub

Private Sub PopulateList(lbTarget As ListBox)
    Dim wombat As ListItem
    Dim myIndex As Long
    
    Clearlist lbTarget
    
    myIndex = 1
    Do
        Set wombat = ListView1.FindItem(Text1.Text, lvwText, myIndex, lvwPartial)
        If Not wombat Is Nothing Then
            myIndex = wombat.Index + 1
            lbTarget.AddItem wombat.Text
        End If
    Loop Until wombat Is Nothing Or myIndex > ListView1.ListItems.Count
    lbTarget.Height = ListHeight * lbTarget.ListCount
    lbTarget.Visible = lbTarget.ListCount
End Sub

Private Sub Clearlist(lbTarget As ListBox)
    Dim myIndex As Long
    
    For myIndex = lbTarget.ListCount - 1 To 0 Step -1
        lbTarget.RemoveItem myIndex
    Next
     
    lbTarget.Visible = False
End Sub[/blue]
 
Thanks Mike, not quite what I was looking for but then again I didn't explain myself very well. I suppose the best way to explain what I'm trying to achieve is 'predictive text' i.e. the full content of the text box is predicted based on what the user has typed into the same text box so far.

ListView1.ListItems.Add , , "Mike"
ListView1.ListItems.Add , , "Mirror"

User enters "mi" into text box which displays as: Mike


[gray]Experience is something you don't get until just after you need it.[/gray]
 
>not quite what I was looking for

However I'd have thought more than sufficient for (the tiny) modification into what you want, particularly since this version handles the issue of more than one match ...
 
More than sufficient for my little programme, thanks. I'd already done something similar after giving up on trying to code my original idea. [wink]

[gray]Experience is something you don't get until just after you need it.[/gray]
 
strongm, any particular reason, why you remove list items in a loop, instead of just calling the List.Clear method?
 
Yes - an artifact of an earlier version in which I was adding a removing individual items from the listbox as the text in the textbox changed. As it now stands .Clear would be more than acceptable
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top