Looping problem

Looping problem

Anonymous
Not applicable
211 Views
6 Replies
Message 1 of 7

Looping problem

Anonymous
Not applicable
Below is the code I have to remove selected items from a list box. The problem is that if you select more than one item, the last item selected always remains. I have tried different loops to accomplish this. Any help would be greatly appreciated.

Private Sub cmdRemoveFiles_Click()
On Error Resume Next
Dim i As Integer
For i = 0 To lstArchiveFiles.ListCount - 1
If lstArchiveFiles.Selected(i) = True Then
lstArchiveFiles.RemoveItem (i)
End If
Next i
End Sub

Thank You,

Jamie Gamauf
0 Likes
212 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
As soon as you remove the first item, the ListCount becomes
ListCount - 1 so you never reach the end of the list unless
the end of the list is the only selected item. I know
there's a way around but I'm drawing a blank at the moment.
Sorry.

--
http://www.acadx.com
0 Likes
Message 3 of 7

Anonymous
Not applicable
Try this, Run the loop backwards that way the index
numbers dont change as you remove items. substitute your FOR statement with
the one below.

 

For i = lstArchiveFiles.ListCount - 1 to
0 Step -1
        

 

Paul H


style="BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
Below
is the code I have to remove selected items from a list box. The problem is
that if you select more than one item, the last item selected always remains.
I have tried different loops to accomplish this. Any help would be greatly
appreciated.

Private Sub cmdRemoveFiles_Click()
    On Error
Resume Next
    Dim i As Integer

    For i = 0 To lstArchiveFiles.ListCount - 1

        If
lstArchiveFiles.Selected(i) = True Then

            lstArchiveFiles.RemoveItem
(i)
        End If

    Next i
End Sub

Thank You,

Jamie Gamauf

0 Likes
Message 4 of 7

Anonymous
Not applicable
Put zero(0) instead of i in this
color=#ff0000>lstArchiveFiles.RemoveItem (i)
line.


Private Sub cmdRemoveFiles_Click()
    On Error
Resume Next
    Dim i As Integer

    For i = 0 To lstArchiveFiles.ListCount - 1

        If
lstArchiveFiles.Selected(i) = True Then

            
color=#ff0000>lstArchiveFiles.RemoveItem (0)


        End If

    Next i
End Sub


--

Madhanagopal
AutoCAD & MCAD Product Support, INDIA
WW Support
& Services, Autodesk
Discussion Q&A:
href="http://www.autodesk.com/discussion">http://www.autodesk.com/discussion



style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

Try this, Run the loop backwards that way the
index numbers dont change as you remove items. substitute your FOR
statement with the one below.

 

For i = lstArchiveFiles.ListCount -
1 to 0 Step -1

        

 

Paul H


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Below
is the code I have to remove selected items from a list box. The problem is
that if you select more than one item, the last item selected always
remains. I have tried different loops to accomplish this. Any help would be
greatly appreciated.

Private Sub cmdRemoveFiles_Click()
    On Error
Resume Next
    Dim i As Integer

    For i = 0 To lstArchiveFiles.ListCount - 1

        If
lstArchiveFiles.Selected(i) = True Then

            lstArchiveFiles.RemoveItem
(i)
        End If

    Next i
End Sub

Thank You,

Jamie Gamauf

0 Likes
Message 5 of 7

Anonymous
Not applicable
> As soon as you remove the first item, the ListCount becomes
> ListCount - 1 so you never reach the end of the list unless
> the end of the list is the only selected item. I know
> there's a way around but I'm drawing a blank at the moment.

Without being able to see the code clearly amongst the " "'s from
what it looks like, you could do this be processing the list instead of
from "0 - list length - 1" you'd use "list length - 1 to 0".

--
Y-------------------------------------------------------------------+
| Darren J. Young | Minnesota CADWorks, Inc. |
| dyoung@mcwi.com | P.O. Box 7293 |
| ftp://ftp.mcwi.com | St. Cloud, Minnesota 56302-7293 |
| http://www.mcwi.com | Phone 1-320-654-9053 |
| CAD/CAM/CNC - Drafting Design Customization Training Programming |
0,0-----------------------------------------------------------------X
Support the amendment to ban commercial unsolicited Email (SPAM) To
join the fight, visit -> http://www.cauce.org/
0 Likes
Message 6 of 7

Anonymous
Not applicable
That would remove the first item in the list whether it is
selected or not.

--
http://www.acadx.com

"Madhanagopal" wrote in message
news:C93332EAC1D1E9FC2549B9044F54F80D@in.WebX.maYIadrTaRb...
> Put zero(0) instead of i in this
lstArchiveFiles.RemoveItem (i) line.
> Private Sub cmdRemoveFiles_Click()
> On Error Resume Next
> Dim i As Integer
> For i = 0 To lstArchiveFiles.ListCount - 1
> If lstArchiveFiles.Selected(i) = True Then
> lstArchiveFiles.RemoveItem (0)
> End If
> Next i
> End Sub
0 Likes
Message 7 of 7

Anonymous
Not applicable
Thanks alot Paul. It works great!

Jamie Gamauf
0 Likes