Solved

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

Posted on 2006-11-29
8
518 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]
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It's better to get text instead of index,

index may change after you add some item into listbox
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 12

Expert Comment

by:jkaios
Comment Utility
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
Comment Utility
'// 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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…

743 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

17 Experts available now in Live!

Get 1:1 Help Now