Solved

Removing item from Listbox Using ItemData

Posted on 2004-09-24
5
161 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
[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
  • 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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.
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…
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…

732 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