listview array problem

F

farmer

I need to populate a listbox of other listview with selected listview
items.although the following code works perfecty I want to use an
Array (for various reasons)

for Each ListItem In listview1.ListItems
If ListItem.Checked = True Then
listbox1.AddItem ListItem.Text
next


So I'm trying to fill an array with listview items. (I need only the
first Item)

Here is The code


Dim varrSel()
Dim i As Integer
Dim cnt As Integer


For Each ListItem In listview1.ListItems

If ListItem.Checked = True Then
cnt = cnt + 1
ReDim Preserve varrSel(1 To cnt)
varrSel(cnt) = ListItem.Text
End If
Next

listbox1.List = varrSel


The only problem is that I can't get it to work

DOES ANYONE HAS A SUGGESTION OF WHAT'S WRONG WITH THE CODE??
 
T

Tom Spink

Hi Farmer,

Yikes!! ReDimming is a very expensive operation, and I don't recommend you
do that. Instead, use an arraylist...

(Also, Enable Option Strict in your project settings)

' ///
Dim alItems As New ArrayList
Dim liItem As ListItem

For Each liItem In ListView1.Items
If liItem.Checked Then alItems.Add(liItem)
Next
' ///

That code populates 'alItems' with the checked items from your listview.

' ///
For Each liItem In alItems
ListBox1.Items.Add(liItem.Text)
Next
' ///

This then add's the items into the listbox.

=======

As for your code...
1. You must declare varrSel as something, that's why you need Option Strict
On.
2. ListItem isn't declared anywhere.
3. The ListBox does not have a 'List' property.
4. The ListView does not have a 'ListItems' property.

=======

This looks suspiciously like VB6 code... You'll have better luck posting to
a VB6 NG (This is VB.NET), try one of the microsoft.public.vb[.*] groups.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"


:
:
: I need to populate a listbox of other listview with selected listview
: items.although the following code works perfecty I want to use an
: Array (for various reasons)
:
: for Each ListItem In listview1.ListItems
: If ListItem.Checked = True Then
: listbox1.AddItem ListItem.Text
: next
:
:
: So I'm trying to fill an array with listview items. (I need only the
: first Item)
:
: Here is The code
:
:
: Dim varrSel()
: Dim i As Integer
: Dim cnt As Integer
:
:
: For Each ListItem In listview1.ListItems
:
: If ListItem.Checked = True Then
: cnt = cnt + 1
: ReDim Preserve varrSel(1 To cnt)
: varrSel(cnt) = ListItem.Text
: End If
: Next
:
: listbox1.List = varrSel
:
:
: The only problem is that I can't get it to work
:
: DOES ANYONE HAS A SUGGESTION OF WHAT'S WRONG WITH THE CODE??
:
:
 
H

Herfried K. Wagner [MVP]

farmer said:
I need to populate a listbox of other listview with selected listview
items.although the following code works perfecty I want to use an
Array (for various reasons)

for Each ListItem In listview1.ListItems
If ListItem.Checked = True Then
listbox1.AddItem ListItem.Text
next


So I'm trying to fill an array with listview items. (I need only the
first Item)

Here is The code


Dim varrSel()
Dim i As Integer
Dim cnt As Integer


For Each ListItem In listview1.ListItems

If ListItem.Checked = True Then
cnt = cnt + 1
ReDim Preserve varrSel(1 To cnt)
varrSel(cnt) = ListItem.Text
End If
Next

listbox1.List = varrSel


The only problem is that I can't get it to work

In VB.NET, arrays have indices 0, ..., n. To create an array with n
elements, you can use 'ReDim Preserve varrSel(n - 1)'. Notice that
changing the number of elements in an array is a very costly process
(every time you do that the array will be copied to a new array and the
old array will be deleted). You can use an 'ArrayList' instead of the
array to get a better performance.
 
F

Fergus Cooney

Hi Farmer,

|| DOES ANYONE HAS A SUGGESTION OF
|| WHAT'S WRONG WITH THE CODE??

YES, I HAVE ONE OR TWO THINGS TO SUGGEST.

[Oh, my poor throat, I can't keep up this shouting. ;-)]

As Tom and Herfried have said, ReDim Preserve is very time-consuming when
done within a loop. If you particularly want an array and not an ArrayList,
it's better to allocate the maximum first (ie. the number of items in the
ListView) and ReDim at the end, when you will know how many items you have.

Note also that you have no Type for your varSel array and they will
therefore be Objects rather than Strings.

In .NET arrays are indexed from 0, not 1. You cannot (ordinarily) change
the lower index of an array. Thus ReDim varrSel (1 To Cnt) is no longer valid
syntax. You would simply use ReDim varrSel (Cnt) - and after the loop not
within.

Regards,
Fergus
 
T

Tom Spink

As I've said aswell, it looks suspiciously like VB5/6 code, e.g. .ListItems
does not exist.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Chaos, Panic, Disorder, my work here is done"


: Hi Farmer,
:
: || DOES ANYONE HAS A SUGGESTION OF
: || WHAT'S WRONG WITH THE CODE??
:
: YES, I HAVE ONE OR TWO THINGS TO SUGGEST.
:
: [Oh, my poor throat, I can't keep up this shouting. ;-)]
:
: As Tom and Herfried have said, ReDim Preserve is very time-consuming
when
: done within a loop. If you particularly want an array and not an
ArrayList,
: it's better to allocate the maximum first (ie. the number of items in the
: ListView) and ReDim at the end, when you will know how many items you
have.
:
: Note also that you have no Type for your varSel array and they will
: therefore be Objects rather than Strings.
:
: In .NET arrays are indexed from 0, not 1. You cannot (ordinarily)
change
: the lower index of an array. Thus ReDim varrSel (1 To Cnt) is no longer
valid
: syntax. You would simply use ReDim varrSel (Cnt) - and after the loop not
: within.
:
: Regards,
: Fergus
:
:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

passing array error 4
Make VBA ComboBox_Change work in 3 sheets 1
System icons in Listview 6
list view question 3
Listbox data 8
ListView Help 1
button array question 3
ListView Control 7

Top