advantagecomputers
asked on
How do I select an item in a VB6 Listbox programatically?
I have a VB6 Listbox which gets refreshed periodically.
What I need to do is check the Listbox to see if any item has been selected.
If so, store the index data of the selected item, refresh the listbox, then reselect the previously selected item.
I'm OK with everything except selecting the item in the Listbox.
Any help would be greatly appreciated.
TIA
What I need to do is check the Listbox to see if any item has been selected.
If so, store the index data of the selected item, refresh the listbox, then reselect the previously selected item.
I'm OK with everything except selecting the item in the Listbox.
Any help would be greatly appreciated.
TIA
ASKER
Thanks for your quick response AngelIII
I have the index data of the selected item, now what I want to do is "highlight" that same item once the listbox is refreshed.
How do I make an item "highlighted" ie, the selected item.
I have the index data of the selected item, now what I want to do is "highlight" that same item once the listbox is refreshed.
How do I make an item "highlighted" ie, the selected item.
As angelIII said, use the ListIndex() property.
Store it in a variable, modify (or update) your ListBox, then set it again:
Dim curIndex As Integer
curIndex = List1.ListIndex
' do something with the ListBox...
List1.ListIndex = curIndex
Store it in a variable, modify (or update) your ListBox, then set it again:
Dim curIndex As Integer
curIndex = List1.ListIndex
' do something with the ListBox...
List1.ListIndex = curIndex
It's better to get text instead of index,
index may change after you add some item into listbox
index may change after you add some item into listbox
I agree with EDDYKT and provided that each text in the list box is unique. If not, then save both the text and the ItemData, but first set the ItemData property to the newly added item so that whenever items are removed from the listbox (which will change the ListIndex value) the ItemData will still remain. This will allow easy lookup when looping thru the list box to set/get the desired item.
'// Form variables in Declaration section
Dim m_iItem As Integer
Dim m_sText As String
Private Sub Form_Load()
Dim ii As Integer
For ii = 1 to 20
PopulateListBox "Item " & ii
Next
End Sub
Sub PopulateListBox(sNewItem As String)
Static iNewItem As Integer
iNewItem = iNewItem + 1
List1.AddItem sNewItem
List1.ItemData(List1.NewIn dex) = iNewItem
End Sub
Private Sub List1_Click()
'Save the currently selected item
m_sText = List1.List(List1.ListIndex )
m_iItem = List1.ItemData(List1.ListI ndex)
End Sub
Sub SelectSavedItem()
Dim ii As Integer
For ii = 0 to Lis1.ListCount - 1
If (List1.List(ii) = m_sText) And (List1.ItemData(ii) = m_iItem) Then
List1.ListIndex = ii
Exit For
End If
Next
End Sub
Dim m_iItem As Integer
Dim m_sText As String
Private Sub Form_Load()
Dim ii As Integer
For ii = 1 to 20
PopulateListBox "Item " & ii
Next
End Sub
Sub PopulateListBox(sNewItem As String)
Static iNewItem As Integer
iNewItem = iNewItem + 1
List1.AddItem sNewItem
List1.ItemData(List1.NewIn
End Sub
Private Sub List1_Click()
'Save the currently selected item
m_sText = List1.List(List1.ListIndex
m_iItem = List1.ItemData(List1.ListI
End Sub
Sub SelectSavedItem()
Dim ii As Integer
For ii = 0 to Lis1.ListCount - 1
If (List1.List(ii) = m_sText) And (List1.ItemData(ii) = m_iItem) Then
List1.ListIndex = ii
Exit For
End If
Next
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's very funny because EDDYKT and my comments were originally pointed out using the "ItemData" property, but never got accepted or even agreed on.
Moreover, the Asker didn't mention that he was pulling data from a database.
Moreover, the Asker didn't mention that he was pulling data from a database.
-1 means -> no item selected.