Solved

Removing item from Listbox Using ItemData

Posted on 2004-09-24
5
154 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

786 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