CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1

    Removing items from listbox

    The following codes return error "invalid property array index"

    If OpenRegistry = true then
    Dim strKeyValue as string
    Dim J as Integer
    for J = 0 to frmRega.lstRegEntries.ListCount - 1
    If frmRega.lstRegEntries.Selected(J) = true then
    strKeyValue = Trim$(Str(J))
    lngReturnValues = RegDeleteValue(hKey, byval strKeyValue)
    If lngReturnValues = ERROR_SUCCESS then
    frmRega.lstRegEntries.RemoveItem (J)
    MsgBox "success!"
    End If
    End If
    next J
    End If



    The item is deleted but error occurs when the integer J is equal to
    the ListCount.

    Thanks,
    Simon


  2. #2
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: Removing items from listbox

    Try looping through the list in reverse. Change this line

    for J = 0 to frmRega.lstRegEntries.ListCount - 1
    to
    for J = frmRega.lstRegEntries.ListCount - 1 to 0 step -1

    The problem with your code is that when an item is removed, the listcount that you had before is smaller by 1, and you try to access an item of the list that no longer exists.


    David Paulson


  3. #3
    Join Date
    Aug 2001
    Location
    New York, USA
    Posts
    169

    Re: Removing items from listbox

    try EXIT FOR - that's just for removing 1 item at a time. is that what you wanted it to do? If so, that will exit the for loop after it's been removed from the list.


    Dim strKeyValue as string
    Dim J as Integer

    for J = 0 to frmRega.lstRegEntries.ListCount -1
    If frmRega.lstRegEntries.Selected(J) then
    strKeyValue = Trim$(Str(J))
    lngReturnValues = RegDeleteValue(hKey, byval strKeyValue)
    If lngReturnValues = ERROR_SUCCESS then
    frmRega.lstRegEntries.RemoveItem (J)
    MsgBox "success!"
    '### Add this line ###
    Exit for
    '#####################
    End If
    End If
    next J




    [email protected]

  4. #4

    Re: Removing items from listbox

    Thanks d.paulson! That was exactly the problem with the code.
    enigmaos, I had thought about using Exit For but the problem was the I couldn't limit the user to delete an entry at a time. Infact they can delete all entries at a time if they want.

    Thanks for the help!
    Simon


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured