Solved

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

Posted on 2006-11-29
8
536 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
8 Comments
 
LVL 142

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now