Results 1 to 9 of 9

Thread: Phonebook style search

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2001
    Posts
    759

    Phonebook style search

    Hi Everyone,

    Currently I have a ListView filled with people's names. I also have a text box on the form. What I would like to do is this. I would like to create a phone book type search where the more letters that are typed into a text box for a persons name the more refined the search becomes. For example, if a person types an 'E' into the text box. The listView displays all people with an 'E' in the first character in there name. If someone types 'Er' into the textbox the search is refined to all people with 'Er' as the first two characters in there name, etc., etc. I currently have the ListView ordered by peoples names. Does anyone know how to accomplish this? I would appreciate any help. Thanks.

  2. #2
    spetnik
    Guest
    If all the data is in an array, then on each keypress, loop through the array for names beginning with the text in the box, clear the list and display the matches.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2001
    Posts
    759
    I have found this bit of information from the MSDN Help, but I cannot figure out how to get it to work right. It works fine if I use the information from the first column in the ListView, however, the people's names are located in the second column of the ListView. This is the column I want to search from. Any ideas on how to fix this? I would appreicate any help. Thanks. Here is the code.

    Code:
    Private Sub Command1_Click()
        Dim objItem As ListItem
        
        Set objItem = lvwMemberInfo.FindItem(Text1.Text, , , lvwPartial)
    
    If objItem Is Nothing Then
        MsgBox "No match found."
        Exit Sub
    Else
        objItem.EnsureVisible
        objItem.Selected = True
        lvwMemberInfo.SetFocus
    End If
    
    End Sub
    
    Private Sub lvwmemberInfo_LostFocus()
        Dim i As Integer
        For i = 1 To lvwMemberInfo.ListItems.Count
            lvwMemberInfo.ListItems.Item(i).Selected = False
        Next i
    End Sub

  4. #4
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Here you go, see if this will work for you.
    VB Code:
    1. Private Sub Form_Load()
    2.     ListView1.ListItems.Add , , "Brunhauser, Mary"
    3.     ListView1.ListItems.Add , , "Brown, Jack"
    4.     ListView1.ListItems.Add , , "Clark, Kenton"
    5.     ListView1.ListItems.Add , , "Cloude, Fred"
    6.     ListView1.ListItems.Add , , "Fredrickson, Francine"
    7.     ListView1.ListItems.Add , , "Woodard, Curt"
    8.     ListView1.ListItems.Add , , "Jankowski, Robert"
    9.     ListView1.ListItems.Add , , "Frinkle, Jennifer"
    10.     ListView1.ListItems.Add , , "Hauser, Scot"
    11.     ListView1.ListItems.Add , , "Alberra, Elmore"
    12.     Text1.Text = ""
    13. End Sub
    14.  
    15. Private Sub Text1_Change()
    16.     Text1.Text = StrConv(Text1.Text, vbProperCase)
    17.     Text1.SelStart = Len(Text1.Text)
    18.     For x = 1 To ListView1.ListItems.Count
    19.         If Left(ListView1.ListItems(x).Text, Len(Text1.Text)) = Text1.Text Then
    20.             ListView1.ListItems(x).EnsureVisible
    21.             ListView1.ListItems(x).Selected = True
    22.             Exit For
    23.         End If
    24.     Next x
    25. End Sub
    You can kill the stuff in the Form_Load() Event, I just put it there for testing.
    -Excalibur

  5. #5
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Change Text1_Change to this:
    VB Code:
    1. Private Sub Text1_Change()
    2.     Text1.Text = StrConv(Text1.Text, vbProperCase)
    3.     Text1.SelStart = Len(Text1.Text)
    4.     [b][i]If Text1.Text = "" Then
    5.         ListView1.ListItems(ListView1.SelectedItem.Index).Selected = False
    6.         Exit Sub
    7.     End If[/i][/b]
    8.     For x = 1 To ListView1.ListItems.Count
    9.         If Left(ListView1.ListItems(x).Text, Len(Text1.Text)) = Text1.Text Then
    10.             ListView1.ListItems(x).EnsureVisible
    11.             ListView1.ListItems(x).Selected = True
    12.             Exit For
    13.         End If
    14.     Next x
    15. End Sub
    This way, if Text1.Text is empty, nothing is selected.
    -Excalibur

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2001
    Posts
    759
    Thanks ExcalibursZone. That code you sent works very well. One other question though. Once an item has been found, I want to be able to press enter on the keyboard and have another form open with more information about that person. The text box still has focus but the ListView has the correct record highlighted. Is there anyway to do this without removing the focus from the text box?

  7. #7
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Very Simply:
    VB Code:
    1. Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = 13 Then
    3.         Form2.Label1.Caption = ListView1.SelectedItem.Text
    4.         Form2.Show
    5.     End If
    6. End Sub
    Or whatever you wish to show based on the text in the listview.
    -Excalibur

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2001
    Posts
    759
    I keep getting an error that says: Operation is not allowed on an object referencing a closed or invalid connection. Then when I debug I shows me the connection string on the second form. Any ideas?

  9. #9
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    What connection string? This should go in form1 and a label (for this example) and only a label should be on form2.
    -Excalibur

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width