472,784 Members | 887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 9789
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
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.