Link to home
Start Free TrialLog in
Avatar of John Gates, CISSP, CDPSE
John Gates, CISSP, CDPSEFlag for United States of America

asked on

The following code is causing random Run-Time Error '35600' Index out of bounds Ok

       

    Dim keepplace As Integer

        keepplace = lv.SelectedItem.Index
   
        PopList

    lv.ListItems(keepplace).Selected = True

This is the code that I use to at a 5 minute interval refresh the items in a listview control.  I cannot reproduce the problem but I have complaints that it is happening.  Anyone ever run into this problem?

I am unsure as to why an error like that would ever come up.  The list is never empty.

Thanks in advance!
-D-
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John Gates, CISSP, CDPSE

ASKER

Good call.  I will put these changes in place and let you know in a couple days 8)
Avatar of Shauli
Shauli

This error will occurs if keepplace is = to 0. That is because listitems cannot begin with a 0. It always begins with 1.
Check to see if keepplace is equal to 0 and trap it if it is.

S
or just make it equal to 1 no?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may have a point here. If 0 means the first elemnet, then yes, make it keepplace+1, otherwise, trap for 0.

S
If it is possible that your users or (other) application code is removing nodes/rows, you will also experience this error on a situational basis.

You might make your application more resilient by using the key of the node instead of its index.  This is analogous to using a recordset bookmark instead of the absoluteposition property.
Another thing... this is why you delete list/array items from back-to-front when referencing items by index number.
Quote>
 If it is possible that your users or (other) application code is removing nodes/rows, you will also experience this error on a situational basis.

You might make your application more resilient by using the key of the node instead of its index.  This is analogous to using a recordset bookmark instead of the absoluteposition property.
</quote>
Absolutely,  Can you explain how to do this "using the key of the node instead of its index"

-D-
Key is a property of a node and must be unique within a listview or treeview.  It is usually assigned a value when you create the node.  The control hashes the key string value and creates a numeric value that is used in a lookup table.

If you can derive some unique value from the row you are adding, it will make your job easier.

==========================
Example:
    ListView1.ListItems.Add , "Now", "Now"
    ListView1.ListItems.Add , "is", "is"
    ListView1.ListItems.Add , "the", "the"
    ListView1.ListItems.Add , "time", "time"

iterating through the listitems:
Debug.Print listview1.ListItems(x).Key, listview1.ListItems(x).index

Now            1
the            3
time           4
is             2
But how would you find it again and select it:

keepplace = lv.SelectedItem.Index

won't work then.
Dim keepplace_Key As String
keepplace_Key = lv.SelectedItem.Key
Using your exact code snippet above:

Dim keepplace_Key As String
keepplace_Key = lv.SelectedItem.Key

MsgBox keepplace_Key

The message box is empty when an item is selected....  Please advise.
<<Please advise>>

If you don't supply a key to your items, there will be no key value.
The items come from a database.  There is an ID field but it is hidden from the list.  So I will make my decision as such.