How to setfocus selected item in listbox

V

Vinod

Hi All,

I've a text box and list box.
After I enter any text in text box the matching item will select in list box.
How do I set focus to selected item in the list since the selected item will
be at any posting ie., either begining or ending or middel.

Please help me on how to set focus (scroll the list till selected item
appears) for selected item in the list.

Advanced Thanks
Vinod
 
K

Ken Snell [MVP]

Assuming that the listbox is not a multiselect listbox, set the listbox's
Value property to the value from the textbox, assuming that the value in the
textbox is one that is in the bound column of the listbox. If this is not
your setup, then tell us more about the listbox's properties, and the code
that you use to "select" the item in the listbox.
 
V

Vinod

Thanks Ken for your response.

I've two tabs in a form and each tab has one text box and one list box(2
columns).

In tab1 I've listbox 1 as multi select and tab2 has single select.

In tab1 and tab2 I'm able select the item which is entered in text box1. but
selected item is not in focus i.e., list box is not scrolling to selected
item.

Here is the fuction used to highlight the item in list box with entered text
in textbox.

'//To highlight preselected list items
'//srcSelCtrl - text box control name
'//strLstCtrl - List box control name
Public Sub Highlight_List_Items(selFrm As Form, srcSelCtrl As String,
strLstCtrl As String)
Dim bolfound As Boolean
Dim lngIncrr As Long

strTemp = Trim(IIf(IsNull(selFrm.Controls(srcSelCtrl).Value) Or
Trim(selFrm.Controls(srcSelCtrl).Value) = "", "",
selFrm.Controls(srcSelCtrl).Value))
If strTemp = "" Then Exit Sub

bolfound = False
For lngIncrr = 0 To selFrm.Controls(strLstCtrl).ListCount
If UCase(Trim(strTemp)) =
UCase(Trim(selFrm.Controls(strLstCtrl).ItemData(lngIncrr))) Then
bolfound = True
Exit For
End If
Next
If bolfound = True Then
selFrm.Controls(strLstCtrl).Selected(lngIncrr) = True
End If
End Sub

Please help me to meet my requirement.

Advanced Thanks
Vinod
 
V

Vinod

Thanks Ken,

Your solution works for me when the list is not multi select.

when list box is multiselect the issue is resolved with following snippet
(to scroll list) after selection an item in listbox.

For I = 0 To Me.LstAccounts.ListCount - 1
If Me.cmbSrchAvlUsr.Value = Me.LstAccounts.ItemData(I) Then
Me.LstAccounts.SetFocus
Me.LstAccounts.ListIndex = I
Exit For
End If
Next I

Once again thanks for your response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top