Solved

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

Posted on 2006-11-29
8
588 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 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

792 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