Solved

Removing item from Listbox Using ItemData

Posted on 2004-09-24
5
158 Views
Last Modified: 2010-05-02
Basically I have two listboxes. When the user clicks the add button, selected items are removed from one and added to the other, it works both ways.

I have no problem adding selected items from one listbox to the other but the problem comes when I try to remove an item. Here is the code:
-----------------------------------------------------------
Dim index As Integer

For index = 0 To ListTables.ListCount - 1

If ListTables.Selected(index) = True Then
    ListSelectedTables.AddItem (ListTables.List(index))
    ListTables.RemoveItem (index)
End If

Next index
-----------------------------------------------------------

The problem occurs after an item is removed. I get an "Invalid propery array index" error when the index is referenced after. I believe that the problem has to do with the fact that the listcount and indexes are changed once an item is removed, but it is still in a loop. Anyway, I know that the item data property is used so that a specific number is assigned to each item so that it does not matter what order they are in and can still be referenced. Problem is I can not remember how to use it through code.

Can someone please give a quick example of using item data to reference items in a listbox and/or if there is a better way without using it.

This question is urgent, thank you.

0
Comment
Question by:fisherbrsnch00
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:anv
ID: 12149201
u need to change your loop use the following instead

dim LstCountas integer

LstCount = ListTables.ListCount - 1

For index = 0 To LstCount

If ListTables.Selected(index) = True Then
    ListSelectedTables.AddItem (ListTables.List(index))
    ListTables.RemoveItem (index)
End If

LstCount = LstTables.ListCount - 1
Next

The LstTables.Listcount That u use in the for loop doesnot change even if u r removing items..either u use While loop or use above loop for ur code and test it..
0
 

Author Comment

by:fisherbrsnch00
ID: 12149231
With that code I get the same error
0
 
LVL 10

Accepted Solution

by:
anv earned 500 total points
ID: 12149246
use the statement

For index = 0 To LstCount

If index >= LstCount Then Exit For

If ListTables.Selected(index) = True Then
    ListSelectedTables.AddItem (ListTables.List(index))
    ListTables.RemoveItem (index)
End If

LstCount = LstTables.ListCount - 1
Next
0
 
LVL 10

Expert Comment

by:anv
ID: 12149250
Missed the statement

If index >= List1.ListCount - 1 Then Exit For

Sorry..:)
0
 

Author Comment

by:fisherbrsnch00
ID: 12149280
Thank you, works great!
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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

856 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