?
Solved

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

Posted on 2006-11-29
8
Medium Priority
?
923 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 86

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
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!

 
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 500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 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