Postion in a List Box

S

Steven

I have a form with a TextBox and a ListBox. The ListBox is based on a query.
As the user types in the TextBox the ListBox will sequentially postion to
the correct record. Here is the code for the Change Event of the TextBox
that makes the sequential search work.

Private Sub Text0_Change()

Dim StrLen As Integer
Dim pos As Integer

If IsNull(Text0.Text) Then Exit Sub

pos = Len(Text0.Text)

For i = 0 To Me.List2.ListCount

If Left(Me.Text0.Text, pos) = Left(Me.List2.ItemData(i), pos) Then 'this
means the strings are equal
Me.List2.Selected(i) = True
Exit For
End If
Next i
Exit Sub

End Sub

I would like to improve on the postion of the selected record in the
ListBox. If the user is in the TextBox and hits a key and the record is not
visible in the current records showing in the ListBox, (Note: 10 records
show at a time in the listbox), then the record is selected in the Listbox
but it shows as the last record in the ListBox. Is there a way to make this
record show more in the middle of the ListBox so the user can see the records
above and below the selected record.

Thank you for your help.


Steven
 
M

Marshall Barton

Steven said:
I have a form with a TextBox and a ListBox. The ListBox is based on a query.
As the user types in the TextBox the ListBox will sequentially postion to
the correct record. Here is the code for the Change Event of the TextBox
that makes the sequential search work.

Private Sub Text0_Change()

Dim StrLen As Integer
Dim pos As Integer

If IsNull(Text0.Text) Then Exit Sub

pos = Len(Text0.Text)

For i = 0 To Me.List2.ListCount

If Left(Me.Text0.Text, pos) = Left(Me.List2.ItemData(i), pos) Then 'this
means the strings are equal
Me.List2.Selected(i) = True
Exit For
End If
Next i
Exit Sub

End Sub

I would like to improve on the postion of the selected record in the
ListBox. If the user is in the TextBox and hits a key and the record is not
visible in the current records showing in the ListBox, (Note: 10 records
show at a time in the listbox), then the record is selected in the Listbox
but it shows as the last record in the ListBox. Is there a way to make this
record show more in the middle of the ListBox so the user can see the records
above and below the selected record.


Sounds like you should be using a combo box instead of a
list box.

If you really have to use a list box, theres a sample
database at www.lebans,com that demonstrates how to do what
you want.
 
S

Steven

I was looking in lebans.com but cant find which one it is. Do you know which
one has this example.

Thanks
 
M

Marshall Barton

Steven said:
I was looking in lebans.com but cant find which one it is. Do you know which
one has this example.


Well it's in there, but with so much stuff on that site,
it's really difficult to find that the sample code is in
ListBoxEnhanced. Not only is it difficult to find but the
code is embedded in a lot of stuff you have no interest in
for this issue. To make matters worse for you, the part you
do want is all done using Windows API procedures. I could
probably get something to work for you, but the last time I
did a scroll bar thing this way, it took me a couple of
weeks to get it right and I won't have that much time
available in the foreseeable future.

I guess my suggesting you look around on Stephen's site
wasn't such a good idea.

I did play around trying to do this just using the ListIndex
property. I wasn't particularly pleased with the result,
but if you want to give it a whirl, here's what I tried.

Private Sub List0_AfterUpdate()
Dim lngIndex As Long
lngIndex = List0.ListIndex
If List0.ListIndex >= List0.ListCount - 3 - 1 Then
List0.ListIndex = List0.ListCount - 1
End If
If lngIndex >= 3 Then
List0.ListIndex = lngIndex - 3
End If
List0.ListIndex = lngIndex
End Sub
 
Top