Solved

How do I select an item in a VB6 Listbox programatically?

Posted on 2006-11-29
8
642 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:advantagecomputers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18036481
the property ListIndex is to be used to specify / get the index of the currently selected item.
-1 means -> no item selected.
0
 

Author Comment

by:advantagecomputers
ID: 18036839
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18037650
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 18038473
It's better to get text instead of index,

index may change after you add some item into listbox
0
 
LVL 12

Expert Comment

by:jkaios
ID: 18040955
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.
0
 
LVL 12

Expert Comment

by:jkaios
ID: 18041054
'// 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.NewIndex) = iNewItem
   End Sub

   Private Sub List1_Click()
      'Save the currently selected item
      m_sText = List1.List(List1.ListIndex)
      m_iItem = List1.ItemData(List1.ListIndex)
   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
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 125 total points
ID: 18060666
well i dont agree on checking the text on the items... or the index cos if u add an item above the selected item, it will change index...

the method in which i usually procede is that i set itemdata for every item (which is normally the primary key of the items in the db, or any other identifier for the objects)

while not rs.eof
 list1.additem rs("productname")
 list1.itemdata(list1.newindex)=rs("productID")
 rs.movenext
wend



if you want to select a specific item then u say: list1.listindex=5

if after changing data u want to refresh the list and reselect the same item... you should store the id of the product in a variable when u click on the listbox

private sub list1_click()
productid=list1.itemdata(list1.listindex)
end sub

and then refresh the list using:

while not rs.eof
 list1.additem rs("productname")
 list1.itemdata(list1.newindex)=rs("productID")
 rs.movenext
wend

dim i as integer
for i=0 to list1.listcount-1
 if list1.itemdata(i)=productid then
  list1.listindex=i
  exit for
 end if
next i

hope this helps

dan
0
 
LVL 12

Expert Comment

by:jkaios
ID: 18161583
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question