|
-
Sep 10th, 2001, 11:55 AM
#1
Thread Starter
Fanatic Member
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.
-
Sep 10th, 2001, 12:30 PM
#2
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.
-
Sep 10th, 2001, 01:02 PM
#3
Thread Starter
Fanatic Member
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
-
Sep 10th, 2001, 03:24 PM
#4
Fanatic Member
Here you go, see if this will work for you.
VB Code:
Private Sub Form_Load()
ListView1.ListItems.Add , , "Brunhauser, Mary"
ListView1.ListItems.Add , , "Brown, Jack"
ListView1.ListItems.Add , , "Clark, Kenton"
ListView1.ListItems.Add , , "Cloude, Fred"
ListView1.ListItems.Add , , "Fredrickson, Francine"
ListView1.ListItems.Add , , "Woodard, Curt"
ListView1.ListItems.Add , , "Jankowski, Robert"
ListView1.ListItems.Add , , "Frinkle, Jennifer"
ListView1.ListItems.Add , , "Hauser, Scot"
ListView1.ListItems.Add , , "Alberra, Elmore"
Text1.Text = ""
End Sub
Private Sub Text1_Change()
Text1.Text = StrConv(Text1.Text, vbProperCase)
Text1.SelStart = Len(Text1.Text)
For x = 1 To ListView1.ListItems.Count
If Left(ListView1.ListItems(x).Text, Len(Text1.Text)) = Text1.Text Then
ListView1.ListItems(x).EnsureVisible
ListView1.ListItems(x).Selected = True
Exit For
End If
Next x
End Sub
You can kill the stuff in the Form_Load() Event, I just put it there for testing.
-
Sep 10th, 2001, 03:39 PM
#5
Fanatic Member
Change Text1_Change to this:
VB Code:
Private Sub Text1_Change()
Text1.Text = StrConv(Text1.Text, vbProperCase)
Text1.SelStart = Len(Text1.Text)
[b][i]If Text1.Text = "" Then
ListView1.ListItems(ListView1.SelectedItem.Index).Selected = False
Exit Sub
End If[/i][/b]
For x = 1 To ListView1.ListItems.Count
If Left(ListView1.ListItems(x).Text, Len(Text1.Text)) = Text1.Text Then
ListView1.ListItems(x).EnsureVisible
ListView1.ListItems(x).Selected = True
Exit For
End If
Next x
End Sub
This way, if Text1.Text is empty, nothing is selected.
-
Sep 10th, 2001, 03:54 PM
#6
Thread Starter
Fanatic Member
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?
-
Sep 10th, 2001, 03:58 PM
#7
Fanatic Member
Very Simply:
VB Code:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Form2.Label1.Caption = ListView1.SelectedItem.Text
Form2.Show
End If
End Sub
Or whatever you wish to show based on the text in the listview.
-
Sep 10th, 2001, 04:12 PM
#8
Thread Starter
Fanatic Member
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?
-
Sep 10th, 2001, 04:44 PM
#9
Fanatic Member
What connection string? This should go in form1 and a label (for this example) and only a label should be on form2.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|