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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Go to specific item in list box via code?

Status
Not open for further replies.

glgcag

MIS
Apr 25, 2001
160
US
I have code that searches for an item in a list box then highlights it. Unfortunately, the list box does not scroll down to the item if it is far down in the list! (Though if I scroll down to the item it IS highlighted.) I'm sure there is a simple way to do this, but I'm stumped. Any suggestions?


Thanks for the help!
 
Try using the list box's Selected property - remember this has a zero-based index, so for example to selected the 15th row in list box lstExample, the code would be something like:
Code:
    lstExample.Selected(14) = True
In my test this has the effect of scrolling.

Hope this helps.

[pc2]
 
I am using the Selected property, just differently- here's my code:

Code:
For i = 0 To Me.lstClients.ListCount
    If sLstSearch = Me.lstClients.Column(2, i) Then
         iLstID = i
         i = Me.lstClients.ListCount
    End If
Next i
Me.lstClients = Me.lstClients.ItemData(iLstID)
Me.lstClients.Selected(iLstID) = True

As you can see, I'm searching for a particular item in the listbox and then when it's found, I set the Selected property to that item. It DOES select the item, but it won't scroll the list box to the item.
 
Even when you set the focus to the listbox before ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yes, I set the focus to the list box prior to running this code. The Selected property works partly, in that it DOES select the proper item, but it does not scroll the list box to make it visible. If I scroll down to the item it is selected though.
 
Is the ListBox MultiSelect ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
It's the reason why.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
So if I had it set to NONE in the properties of the listbox, it would go directly to the record? That makes sense when I think about how in MultiSelect mode the list box can have items selected that are out of view. Hmmm. What about setting the multiselect mode via events on the form? If I open the form with the listbox set to NONE for multiselect and then if the user decides not to use what I have selected for them, then I could set it to SIMPLE multiselect . . .

What event would be best for setting to SIMPLE? If the user clicks into the listbox?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top