Listbox problems

Listbox problems

Anonymous
Not applicable
175 Views
2 Replies
Message 1 of 3

Listbox problems

Anonymous
Not applicable
I'm a novice VB / VBA person and I've run into yet another roadblock.

I'm trying to move a group of drawing files from one listbox (that displays
the total list of files in a subdirectory) to another. This second listbox
is the group of drawings I'm going to perform the operation on.

the problem is: I keep getting an "invalid property array index" error
(code 381) in the section of code that removes the selected items (after its
added to the second listbox). My code, as far as I know, is in accordance
with every book and reference I've seen. What am I doing wrong ?

Dim i As Integer
For i = 0 To filelist.ListCount - 1
If filelist.Selected(i) Then
lstDrawings.AddItem dirname & filelist.List(i)
End If
Next i

Dim k As Integer
For k = 0 To filelist.SelCount - 1
If filelist.Selected Then <<< this is where the error
code is initiated
filelist.RemoveItem (k)
End If
Next k

The second section of code is where the error occurs (see my comment to the
right of the code).

If you have a suggestion, please email: nimzondn@mindspring.com
or just post your recommendations
thanks
jm
0 Likes
176 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
hi john,

the problem at this point is that you don't give an index to selected
if filelist.Selected(k) 'should be the syntax

then you run in to more problems
first is that you have to count not to (SelCount-1) but to (ListCount -1)
second problem is, that vb evaluates the listcount at the beginning of the
loop and not during running in the loop, as you delete items in the list you
will get an index-error. ie your loop starts with values from 0 to 3, you
delete listitem 2, then 3 is missing and your loop fails. you can return the
loop if you do
for i=(filelist.ListCount -1) to 0 step -1

then your job should be done

regards, alfred
--
___________________________________________________________
Alfred Neswadba
Ingenieur Studio HOLLAUS
A-3100 St.Poelten, Brandstroemg. 6..... +43 699 21807303
Internet ................................ www.hollaus.at
E-Mail ...................... alfred.neswadba@hollaus.at
___________________________________________________________

"john" schrieb im Newsbeitrag
news:2D9D26622E20ECB3DCB5D7BC738B3B58@in.WebX.maYIadrTaRb...
> I'm a novice VB / VBA person and I've run into yet another roadblock.
>
> I'm trying to move a group of drawing files from one listbox (that
displays
> the total list of files in a subdirectory) to another. This second
listbox
> is the group of drawings I'm going to perform the operation on.
>
> the problem is: I keep getting an "invalid property array index" error
> (code 381) in the section of code that removes the selected items (after
its
> added to the second listbox). My code, as far as I know, is in accordance
> with every book and reference I've seen. What am I doing wrong ?
>
> Dim i As Integer
> For i = 0 To filelist.ListCount - 1
> If filelist.Selected(i) Then
> lstDrawings.AddItem dirname & filelist.List(i)
> End If
> Next i
>
> Dim k As Integer
> For k = 0 To filelist.SelCount - 1
> If filelist.Selected Then <<< this is where the error
> code is initiated
> filelist.RemoveItem (k)
> End If
> Next k
>
> The second section of code is where the error occurs (see my comment to
the
> right of the code).
>
> If you have a suggestion, please email: nimzondn@mindspring.com
> or just post your recommendations
> thanks
> jm
>
0 Likes
Message 3 of 3

Anonymous
Not applicable
It worked wonderfully. I appreciate the help.

I'm amazed at the quality of help I recieve in this discussion group.

john

Alfred Neswadba wrote in message
news:E397A02D6F34DF1B5D69D783EBB6675D@in.WebX.maYIadrTaRb...
> hi john,
>
> the problem at this point is that you don't give an index to selected
> if filelist.Selected(k) 'should be the syntax
>
> then you run in to more problems
> first is that you have to count not to (SelCount-1) but to (ListCount -1)
> second problem is, that vb evaluates the listcount at the beginning of the
> loop and not during running in the loop, as you delete items in the list
you
> will get an index-error. ie your loop starts with values from 0 to 3, you
> delete listitem 2, then 3 is missing and your loop fails. you can return
the
> loop if you do
> for i=(filelist.ListCount -1) to 0 step -1
>
> then your job should be done
>
> regards, alfred
> --
> ___________________________________________________________
> Alfred Neswadba
> Ingenieur Studio HOLLAUS
> A-3100 St.Poelten, Brandstroemg. 6..... +43 699 21807303
> Internet ................................ www.hollaus.at
> E-Mail ...................... alfred.neswadba@hollaus.at
> ___________________________________________________________
>
> "john" schrieb im Newsbeitrag
> news:2D9D26622E20ECB3DCB5D7BC738B3B58@in.WebX.maYIadrTaRb...
> > I'm a novice VB / VBA person and I've run into yet another roadblock.
> >
> > I'm trying to move a group of drawing files from one listbox (that
> displays
> > the total list of files in a subdirectory) to another. This second
> listbox
> > is the group of drawings I'm going to perform the operation on.
> >
> > the problem is: I keep getting an "invalid property array index" error
> > (code 381) in the section of code that removes the selected items (after
> its
> > added to the second listbox). My code, as far as I know, is in
accordance
> > with every book and reference I've seen. What am I doing wrong ?
> >
> > Dim i As Integer
> > For i = 0 To filelist.ListCount - 1
> > If filelist.Selected(i) Then
> > lstDrawings.AddItem dirname & filelist.List(i)
> > End If
> > Next i
> >
> > Dim k As Integer
> > For k = 0 To filelist.SelCount - 1
> > If filelist.Selected Then <<< this is where the error
> > code is initiated
> > filelist.RemoveItem (k)
> > End If
> > Next k
> >
> > The second section of code is where the error occurs (see my comment to
> the
> > right of the code).
> >
> > If you have a suggestion, please email: nimzondn@mindspring.com
> > or just post your recommendations
> > thanks
> > jm
> >
>
0 Likes