473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

listview array problem



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??
Nov 20 '05 #1
2 9817
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"
"farmer" <an*******@hotmail.vom> wrote in message
news:ss********************************@4ax.com...
:
:
: 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??
:
:
Nov 20 '05 #2
farmer <an*******@hotmail.vom> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
6
by: Beginner | last post by:
Hi, I'm trying to populate a TreeView from an existing and filled in ListView (lvwBuild). lvwBuild is a basic two column listview which can have any number of rows. I would like the first...
4
by: firefox | last post by:
Hola Foro!, tengo una rutina que permite agregar varios elementos o items de una sola vez en un ListView. Lo que sucede es que en el momento que se añaden los items al ListView se pone todo blanco,...
5
by: Phill W. | last post by:
(VB'2003) What's the correct way to remove multiple, selected items from a ListView control (say, from a ContextMenu)? I ask because I'm getting a very annoying ArgumentOutOfRangeException...
12
by: garyusenet | last post by:
I have had no replies to my previous post so perhaps I didn't write it good enough. Please excuse new thread but i wanted to break from the last thread hopefully this thread will be better. ...
0
by: Griff | last post by:
I have a Windows appliction that I want to turn into a WPF application (as a proof of concept). However, I have encountered a problem with the ListView object and I can't find any suitable help...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a ListView in which each item is an array of 5 strings. I add these string arrays to the ListView by doing the following, where "result" is the array of 5 strings to add: ...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, In my application I need to periodically remove all current items from a ListView and add a new set into it. The following abbreviated code contains the basic idea: private void...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.